Solved

Compressing PDFs using SharpZipLib not working

Posted on 2006-11-15
3
562 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

Question has a verified solution.

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

I recently went through the process of creating a Calendar Control of events with the basis of using a database to keep track of the dates that are selectable, one requirement was to have the selected date pop-up in a simple lightbox.  At first this…
User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
In this video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

707 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