views:

1712

answers:

6

I have the following method in my code:

private bool GenerateZipFile(List<FileInfo> filesToArchive, DateTime archiveDate)
{
    try
    {
        using (ZipOutputStream zipStream = new ZipOutputStream(File.Create(GetZipFileName(archiveDate))))
        {
            zipStream.SetLevel(9); // maximum compression.
            byte[] buffer = new byte[4096];

            foreach (FileInfo fi in filesToArchive)
            {
                string fileName = ZipEntry.CleanName(fi.Name);
                ZipEntry entry = new ZipEntry(fileName);
                entry.DateTime = fi.LastWriteTime;
                zipStream.PutNextEntry(entry);

                using (FileStream fs = File.OpenRead(fi.FullName))
                {
                    StreamUtils.Copy(fs, zipStream, buffer);
                }

                zipStream.CloseEntry();
            }

            zipStream.Finish();
            zipStream.Close();
        }
        return true; 
    }
    catch (Exception ex)
    {
        OutputMessage(ex.ToString());
        return false;
    }
}

This code generates a ZIP file with all the correct entries, but each file is listed as being 4 TB (both unpacked and packed) and creates the following error when I try to open it:

Extracting to "C:\winnt\profiles\jbladt\LOCALS~1\Temp\"
Use Path: no   Overlay Files: yes
skipping: QPS_Inbound-20081113.txt: this file is not in the standard Zip 2.0 format
   Please see www.winzip.com/zip20.htm for more information
error:  no files were found - nothing to do

The code is practically taken from the samples, but I seem to be missing something. Does anyone have any pointers?

+1  A: 

I had a similar problem which I solved by specifying the CompressionMethod and CompressedSize properties on the ZipEntry object. In my usage, however, the zip was being created to group several very small files in one download rather than actually compressing the files, so I got away with using no compression (level 0) and using the file's actual size for the CompressedSize property. Not sure how that would work if compression is necessary.

Tinister
A: 

Do you get the same result for other values of SetLevel?

devio
+6  A: 

I used to use SharpZipLib until I switched to DotNetZip You may want to check it out as an alternative.

Example:

try
   {
     using (ZipFile zip = new ZipFile("MyZipFile.zip")
     {
       zip.AddFile("c:\\photos\\personal\\7440-N49th.png");
       zip.AddFile("c:\\Desktop\\2005_Annual_Report.pdf");
       zip.AddFile("ReadMe.txt");
       zip.Save();
     }
   }
   catch (System.Exception ex1)
   {
     System.Console.Error.WriteLine("exception: " + ex1);
   }
Logicalmind
This code is so much simpler.
Cheeso
A: 

For the benefit of anyone having the same problem in the future: My problem turned out to be that I was using a truly ancient version of WinZip (8.0, I think) to view the files. Using a modern viewer (12.0) solved the problem.

Jekke
A: 

See the post by Tyler Holmes

The issue with Winzip 8.0 and others is with Zip64. Set the original file size when adding the ZipEntry and the error goes away.

e.g.

string fileName = ZipEntry.CleanName(fi.Name);
ZipEntry entry = new ZipEntry(fileName);
entry.DateTime = fi.LastWriteTime;
entry.Size = fi.Length;
zipStream.PutNextEntry(entry);

Current release zip utilities don't have the problem.

A: 

hi, I got error "this file is not in the standard Zip 2.0 format" while unzip zipped file manually.

I am using ICSharpCode.SharpZipLib dll to zip the file using .Net 2.0 C#. when i use version 0.81 then its work nicely but for version 0.85 and 0.86 it will give me above error. using 0.81 we can zip the file up 2.15gb only and i want to zip more than 2gb folder.

Please help me as earlry as possible... Thanks in advance.

koolprasad2003