Compressing PDFs using SharpZipLib not working
Posted on 2006-11-15
I'm a newbie to SharpZipLib, so please excuse the seeming idiocy...
I've written some code which will, supposedly, take a PDF, and place it in a zip file.. I used the examples. After the file is saved, I try and view it with WinZip or the standard Windows Compressed (zip) Folders utility and both are corrupted. When I view the file information in WinZip, it tells me:
When I try and view the file from within WinZip, I get an error: skipping: AnnualReport2005.pdf Need PK compat. v4.5 (can do v2.0). Please press F1 for help.
Checking the file from Windows Compressed Folder utility tells me that the file size is 4,194,304 KB and the ratio is 100%!
I am unable to open the file in either application.
As a side note, I also tried to use the same code against a .TXT file and found that it could be viewed using the Windows utility but not with WinZip.
My code is:
using (ZipOutputStream s = new ZipOutputStream(File.Create("C:\\Inetpub\\wwwroot\\FI_ICN\\TempZips\\ziptest.zip")))
s.SetLevel(9); // 0 - store only to 9 - means best compression
byte buffer = new byte;
ZipEntry entry = new ZipEntry("AnnualReport2005.pdf");
entry.DateTime = DateTime.Now;
using (FileStream fs = File.OpenRead("C:\\Inetpub\\wwwroot\\FI_ICN\\kv_plu\\AnnualReport2005.PDF"))
sourceBytes = fs.Read(buffer, 0, buffer.Length);
s.Write(buffer, 0, sourceBytes);
} while (sourceBytes > 0);
Response.ContentType = "application/zip";
What am I doing wrong? I'm getting kinda desperate here. I've tried FileStreams, but thought it may have something to do with using a memory stream instead. If this is the case, an example would be greatly appreciated.