Solved

Compressing PDFs using SharpZipLib not working

Posted on 2006-11-15
3
539 Views
Last Modified: 2012-06-21
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:

Name: AnnualReport2005.pdf
Date: 11/15/06
Time: 22:43
Size: ?
Ratio: ?
Packed: ?

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 ICSharpCode.SharpZipLib.Zip;


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[4096];

ZipEntry entry = new ZipEntry("AnnualReport2005.pdf");

entry.DateTime = DateTime.Now;

s.PutNextEntry(entry);

using (FileStream fs = File.OpenRead("C:\\Inetpub\\wwwroot\\FI_ICN\\kv_plu\\AnnualReport2005.PDF"))

{

int sourceBytes;

do

{

sourceBytes = fs.Read(buffer, 0, buffer.Length);

s.Write(buffer, 0, sourceBytes);

} while (sourceBytes > 0);

}

s.Finish();
s.Close();

}

Response.ContentType = "application/zip";
Response.AddHeader("Content-Disposition", "inline;filename=ziptest.zip");
Response.WriteFile("C:\\Inetpub\\wwwroot\\FI_ICN\\TempZips\\ziptest.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.

Jordan Stradtman
0
Comment
Question by:jstradtman
3 Comments
 
LVL 5

Accepted Solution

by:
rajups earned 500 total points
Comment Utility
Hi,
Try the belwo code

 Dim s As ZipOutputStream = New ZipOutputStream(File.Create(C:\\Inetpub\\wwwroot\\FI_ICN\\TempZips\\ziptest.zip))
                s.SetLevel(5)
                AddToZip("C:\\Inetpub\\wwwroot\\FI_ICN\\kv_plu\\AnnualReport2005.PDF", s, 1)
                s.Finish()
                s.Close()

-----------------------------------------------------------------------------------------------------------------------------------------

 Private Sub AddToZip(ByVal file As String, ByVal s As ZipOutputStream, ByVal i As Integer)
            Dim fs As FileStream = IO.File.OpenRead(file)
            Dim buffer(fs.Length) As Byte
            fs.Read(buffer, 0, buffer.Length)
            Dim entry As ZipEntry = New ZipEntry(System.IO.Path.GetFileName(file))
            'Dim entry As ZipEntry = New System.IO.Path.GetFileName(fullpath)

            entry.IsCrypted = False
            'ZipEntry.CleanName(file)
            s.PutNextEntry(entry)
            s.Write(buffer, 0, buffer.Length)
            System.Math.Min(System.Threading.Interlocked.Increment(i), i - 1)
 End Sub

Ucan even add multiple (PDF) files 2 a single zip stream using the above function also.




and also use compression level as 5 than 9.

All d best,
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

In an ASP.NET application, I faced some technical problems. In this article, I list them out and show the solutions that I found.  I hope it will be useful. Problem: After closing a pop-up window, the parent page should be refreshed automaticall…
Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now