Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Creating incomplete zip when using zip64. #869

Open
aryanchauhannn opened this issue May 20, 2024 · 0 comments
Open

Creating incomplete zip when using zip64. #869

aryanchauhannn opened this issue May 20, 2024 · 0 comments
Labels
bug zip Related to ZIP file format

Comments

@aryanchauhannn
Copy link

Describe the bug

Hey, I'm trying to create a zip which uses zip64 format (zip64 extra fields) always for all file size (i.e. also for size < 4GB) with compression set to store. I'm able to create the zip but it is missing 'Relate header offset in central directory headers' and also missing 'Zip64 end of central directory record' and 'Zip64 end of central directory locator'. Please help.

I'm using this code.

using System;
using System.IO;
using ICSharpCode.SharpZipLib.Zip;

class Program
{
static void Main(string[] args)
{
// Specify the directory to zip
string directoryToZip = @"G:\New folder\Aryanolm";

// Specify the output zip file
string zipFilePath = @"G:\New folder\20may.olm";

// Zip the directory
ZipDirectory(directoryToZip, zipFilePath);

Console.WriteLine("Directory zipped successfully.");

}

static void ZipDirectory(string directoryToZip, string zipFilePath)
{
// Create a FileStream to write the zip file
using (FileStream fsOut = File.Create(zipFilePath))
{
// Create a ZipOutputStream with Zip64 support enabled
using (ZipOutputStream zipStream = new ZipOutputStream(fsOut))
{
zipStream.UseZip64 = UseZip64.On;

        // Set compression level to Store (no compression)
        zipStream.SetLevel(0);

        // Recursively zip the directory
        ZipDirectory(directoryToZip, zipStream, "");

        // Finish writing the zip file
        zipStream.Finish();
    }
}

}

static void ZipDirectory(string directory, ZipOutputStream zipStream, string parentDirectory)
{
// Get all files and directories within the directory
string[] files = Directory.GetFiles(directory);
string[] subdirectories = Directory.GetDirectories(directory);

// Add files to the zip
foreach (string file in files)
{
    AddFileToZip(zipStream, file, parentDirectory);
}

// Recursively add subdirectories to the zip
foreach (string subdirectory in subdirectories)
{
    string directoryName = Path.GetFileName(subdirectory);
    ZipDirectory(subdirectory, zipStream, Path.Combine(parentDirectory, directoryName));
}

}

static void AddFileToZip(ZipOutputStream zipStream, string file, string parentDirectory)
{
// Create a new ZipEntry
string entryName = Path.Combine(parentDirectory, Path.GetFileName(file));
ZipEntry entry = new ZipEntry(entryName);

// Set compression method to Store (no compression)
entry.CompressionMethod = CompressionMethod.Stored;

// Set the last modified time of the file
entry.DateTime = DateTime.Now;

// Add the entry to the zip stream
zipStream.PutNextEntry(entry);

// Write the file content to the zip stream
using (FileStream fs = File.OpenRead(file))
{
    byte[] buffer = new byte[4096];
    int bytesRead;
    while ((bytesRead = fs.Read(buffer, 0, buffer.Length)) > 0)
    {
        zipStream.Write(buffer, 0, bytesRead);
    }
}

// Close the entry
zipStream.CloseEntry();

}
}

Reproduction Code

using System; using System.IO; using ICSharpCode.SharpZipLib.Zip; class Program { static void Main(string[] args) { // Specify the directory to zip string directoryToZip = @"G:\New folder\Aryanolm"; // Specify the output zip file string zipFilePath = @"G:\New folder\20may.olm"; // Zip the directory ZipDirectory(directoryToZip, zipFilePath); Console.WriteLine("Directory zipped successfully."); } static void ZipDirectory(string directoryToZip, string zipFilePath) { // Create a FileStream to write the zip file using (FileStream fsOut = File.Create(zipFilePath)) { // Create a ZipOutputStream with Zip64 support enabled using (ZipOutputStream zipStream = new ZipOutputStream(fsOut)) { zipStream.UseZip64 = UseZip64.On; // Set compression level to Store (no compression) zipStream.SetLevel(0); // Recursively zip the directory ZipDirectory(directoryToZip, zipStream, ""); // Finish writing the zip file zipStream.Finish(); } } } static void ZipDirectory(string directory, ZipOutputStream zipStream, string parentDirectory) { // Get all files and directories within the directory string[] files = Directory.GetFiles(directory); string[] subdirectories = Directory.GetDirectories(directory); // Add files to the zip foreach (string file in files) { AddFileToZip(zipStream, file, parentDirectory); } // Recursively add subdirectories to the zip foreach (string subdirectory in subdirectories) { string directoryName = Path.GetFileName(subdirectory); ZipDirectory(subdirectory, zipStream, Path.Combine(parentDirectory, directoryName)); } } static void AddFileToZip(ZipOutputStream zipStream, string file, string parentDirectory) { // Create a new ZipEntry string entryName = Path.Combine(parentDirectory, Path.GetFileName(file)); ZipEntry entry = new ZipEntry(entryName); // Set compression method to Store (no compression) entry.CompressionMethod = CompressionMethod.Stored; // Set the last modified time of the file entry.DateTime = DateTime.Now; // Add the entry to the zip stream zipStream.PutNextEntry(entry); // Write the file content to the zip stream using (FileStream fs = File.OpenRead(file)) { byte[] buffer = new byte[4096]; int bytesRead; while ((bytesRead = fs.Read(buffer, 0, buffer.Length)) > 0) { zipStream.Write(buffer, 0, bytesRead); } } // Close the entry zipStream.CloseEntry(); } }

Steps to reproduce

using System;
using System.IO;
using ICSharpCode.SharpZipLib.Zip;

class Program
{
static void Main(string[] args)
{
// Specify the directory to zip
string directoryToZip = @"G:\New folder\Aryanolm";

    // Specify the output zip file
    string zipFilePath = @"G:\New folder\20may.olm";

    // Zip the directory
    ZipDirectory(directoryToZip, zipFilePath);

    Console.WriteLine("Directory zipped successfully.");
}

static void ZipDirectory(string directoryToZip, string zipFilePath)
{
    // Create a FileStream to write the zip file
    using (FileStream fsOut = File.Create(zipFilePath))
    {
        // Create a ZipOutputStream with Zip64 support enabled
        using (ZipOutputStream zipStream = new ZipOutputStream(fsOut))
        {
            zipStream.UseZip64 = UseZip64.On;

            // Set compression level to Store (no compression)
            zipStream.SetLevel(0);

            // Recursively zip the directory
            ZipDirectory(directoryToZip, zipStream, "");

            // Finish writing the zip file
            zipStream.Finish();
        }
    }
}

static void ZipDirectory(string directory, ZipOutputStream zipStream, string parentDirectory)
{
    // Get all files and directories within the directory
    string[] files = Directory.GetFiles(directory);
    string[] subdirectories = Directory.GetDirectories(directory);

    // Add files to the zip
    foreach (string file in files)
    {
        AddFileToZip(zipStream, file, parentDirectory);
    }

    // Recursively add subdirectories to the zip
    foreach (string subdirectory in subdirectories)
    {
        string directoryName = Path.GetFileName(subdirectory);
        ZipDirectory(subdirectory, zipStream, Path.Combine(parentDirectory, directoryName));
    }
}

static void AddFileToZip(ZipOutputStream zipStream, string file, string parentDirectory)
{
    // Create a new ZipEntry
    string entryName = Path.Combine(parentDirectory, Path.GetFileName(file));
    ZipEntry entry = new ZipEntry(entryName);

    // Set compression method to Store (no compression)
    entry.CompressionMethod = CompressionMethod.Stored;

    // Set the last modified time of the file
    entry.DateTime = DateTime.Now;

    // Add the entry to the zip stream
    zipStream.PutNextEntry(entry);

    // Write the file content to the zip stream
    using (FileStream fs = File.OpenRead(file))
    {
        byte[] buffer = new byte[4096];
        int bytesRead;
        while ((bytesRead = fs.Read(buffer, 0, buffer.Length)) > 0)
        {
            zipStream.Write(buffer, 0, bytesRead);
        }
    }

    // Close the entry
    zipStream.CloseEntry();
}

}

Expected behavior

Hey, I'm trying to create a zip which uses zip64 format (zip64 extra fields) always for all file size (i.e. also for size < 4GB) with compression set to store. I'm able to create the zip but it is missing 'Relate header offset in central directory headers' and also missing 'Zip64 end of central directory record' and 'Zip64 end of central directory locator'. Please help.

Operating System

Windows

Framework Version

.NET 5

Tags

ZIP

Additional context

No response

@github-actions github-actions bot added the zip Related to ZIP file format label May 20, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug zip Related to ZIP file format
Projects
None yet
Development

No branches or pull requests

1 participant