Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Basics of SharpZipLib. What am I missing?

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?

like image 801
Yes - that Jake. Avatar asked Dec 18 '22 10:12

Yes - that Jake.


1 Answers

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);
   }
like image 82
Logicalmind Avatar answered Dec 24 '22 00:12

Logicalmind