Solved

Compressing PDFs using SharpZipLib not working

Posted on 2006-11-15
3
553 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
ID: 17953756
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

Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Winform Module - What is the ASP.Net equiv 2 40
Code works but breaks when I add one section 4 31
Can Angular be used with classic asp.net? 3 34
Data organization issue 7 39
One of the pain points with developing AJAX, JavaScript, JQuery, and other client-side behaviors is that JavaScript doesn’t allow for cross domain request for pulling content. For example, JavaScript code on www.johnchapman.name could not pull conte…
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 …
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

730 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