+1 vote
by (210 points)

    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
by (75.8k points)
selected by
 
Best answer

// 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);
}

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.

by (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
by (75.8k 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);
    }
by (210 points)
Actually, it works!!! Thank you very much, you guys are the best!
by (75.8k 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?
by (210 points)
Hey Lukas, I will do this exercise when I come back from vacation next week
by (75.8k points)
Thank you, we very appreciate it.
...