?
Solved

Compressing PDFs using SharpZipLib not working

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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

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…
ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses

649 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