Unable to zip and encrypt 100 MB file

+1 vote
asked Dec 2, 2020 by mpodolski (210 points)

We have a simple code that tries zip and encrypt a file. That code works perfectly for the moderately size files. However, when we try to encrypt and zip the file that has the size of 100MB, the contents of the file are lost in a process and the Rebex ends up zipping an empty file.

Is there a catch for handling large files?

Thank you in advance, here is our code

    using (ZipArchive zip = new ZipArchive(ZipFilePath, ArchiveOpenMode.Create))
    {
        // Set the Password first
        zip.Password = strUserPIN;

        // Change the default Encryption algorithm 
        zip.EncryptionAlgorithm = useAes256EncryptionForWinZip == "YES" ?
            Rebex.IO.Compression.EncryptionAlgorithm.Aes256 : Rebex.IO.Compression.EncryptionAlgorithm.Zip20;


        // Add the file to newly created "files" folder within the zip file
        zip.AddFile(Temp_BPI_SaveLocation + strDataFileWithTimeStamp, @"\files\");

        //Save the Zip file
        zip.Save();

        // cloase the zip file
        zip.Close();

    }

1 Answer

0 votes
answered Dec 3, 2020 by Lukas Matyska (58,750 points)
selected Dec 10, 2020 by mpodolski
 
Best answer

There is only 4GB size limit for standard ZIP format. There should be no issues for files around 100MB.

Can you please try to add the file using FileStream and print its length and position at the start and at the end of the process?
Printing AddFile's result and ZipItem's properties would be useful as well.
You can do it like this:

// Add the file to newly created "files" folder within the zip file
using (var file = File.OpenRead(Temp_BPI_SaveLocation + strDataFileWithTimeStamp))
{
    Console.WriteLine("Adding file of size: {0} starting at position: {1}.", file.Length, file.Position);
    var result = zip.AddFile(file, @"\files\" + strDataFileWithTimeStamp);
    Console.WriteLine("Added file of size: {0} ended at position: {1}.", file.Length, file.Position);
    Console.WriteLine("Result: {0}, {1}, {2}.", result.FilesAffected, result.FilesCompressedLength, result.FilesUncompressedLength);

    var item = zip[@"\files\" + strDataFileWithTimeStamp];
    if (item == null)
        Console.WriteLine("File not found in the Zip file.");
    else
        Console.WriteLine("Zip file lengths: {0}, {1}.", item.CompressedLength, item.Length);
}

I have tried the code above for 180MB file and the output looks like this:

Adding file of size: 181102014 starting at position: 0.
Added file of size: 181102014 ended at position: 181102014.
Result: 1, 180479584, 181102014.
Zip file lengths: 180479584, 181102014.

The file is present in the ZIP archive, it is non-empty and it can be decrypted successfully.

Please, post here:

  1. the output of modified code
  2. version of assemblies you are using
commented Dec 3, 2020 by mpodolski (210 points)
Thank you very much for your comment

Here is the output

Adding file of size: 103968768 starting at position: 0.
Added file of size: 103968768 ended at position: 103968768.
Result: 1, 135015, 103968768.
Zip file lengths: 135015, 103968768

Here is the version we use

Runtime version: v4.0.30319
Version: 2.0.6083.0

Thank you very much in advance
commented Dec 3, 2020 by Lukas Matyska (58,750 points)
From the output it seems that the file was read completely.
Also, it seems that the file is present in the ZIP archive and has non-empty length.

Can you please verify that the file in the ZIP file is non-empty and can be extracted from the archive?

Can you please also try to extract the file programmatically using the following code?

    using (ZipArchive zip = new ZipArchive(ZipFilePath, ArchiveOpenMode.Open))
    {
        zip.Password = strUserPIN;

        var ms = new MemoryStream();
        zip.ExtractFile(@"\files\" + strDataFileWithTimeStamp, ms);

        Console.WriteLine("Length of extracted file: {0}", ms.Length);
    }
commented Dec 4, 2020 by mpodolski (210 points)
Actually, it works!!! Thank you very much, you guys are the best!
commented Dec 4, 2020 by Lukas Matyska (58,750 points)
Nice. So we have a working workaround, but the original issue seems to be still present.
Can you please help us to find out what is happening?

The version 2.0.6083.0 you are using is 4 years old, so it is possible that the issue is already solved. Unfortunately, I am not able to reproduce the issue with the 2.0.6083.0 version nor the latest version.

Can you please try the similar as before, but now use the original call: zip.AddFile(Temp_BPI_SaveLocation + strDataFileWithTimeStamp, @"\files\").

The test code should look like this:

    // Add the file to newly created "files" folder within the zip file
    var result = zip.AddFile(Temp_BPI_SaveLocation + strDataFileWithTimeStamp, @"\files\");
    Console.WriteLine("Result: {0}, {1}, {2}.", result.FilesAffected, result.FilesCompressedLength, result.FilesUncompressedLength);

    var item = zip[@"\files\" + strDataFileWithTimeStamp];
    if (item == null)
        Console.WriteLine("File not found in the Zip file.");
    else
        Console.WriteLine("Zip file lengths: {0}, {1}.", item.CompressedLength, item.Length);

What is the program output now?
Are you able to extract the file or is it empty?
Are you able to extract the file programmatically as mentioned before?
commented Dec 10, 2020 by mpodolski (210 points)
Hey Lukas, I will do this exercise when I come back from vacation next week
commented Dec 10, 2020 by Lukas Matyska (58,750 points)
Thank you, we very appreciate it.
...