?
Solved

Compressing PDFs using SharpZipLib not working

Posted on 2006-11-15
3
Medium Priority
?
564 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 2000 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This article discusses the ASP.NET AJAX ModalPopupExtender control. In this article we will show how to use the ModalPopupExtender control, how to display/show/call the ASP.NET AJAX ModalPopupExtender control from javascript, how to show/display/cal…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
Suggested Courses

777 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