473,320 Members | 2,180 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Unzip problem with images

Hi to all,

I am using ICSharpCode.SharpZipLib.Zip.

My zip file contains a number of images which when they get unzipped only
contain a partial image. The filesize looks correct.

The unziping is done below it is not clear where the problem is. For int
numberOfBytesToRead = (int) zipEntryObject.Size; I initially had the
int numberOfBytesToRead = (int) readStream.length but this always gave me
0.

foreach (ZipEntry zipEntryObject in zipFileObject)
{
System.IO.Stream readStream =
zipFileObject.GetInputStream(zipEntryObject);
int numberOfBytesToRead = (int) zipEntryObject.Size;

byte[] bytes = new byte[numberOfBytesToRead];

readStream.Read(bytes, 0, numberOfBytesToRead);
System.IO.FileStream outputStream =
new System.IO.FileStream(@Directory + zipEntryObject.Name,
System.IO.FileMode.Create);

outputStream.Write(bytes, 0, numberOfBytesToRead);
outputStream.Flush();
outputStream.Close();

}

Sham.
May 25 '06 #1
1 1235
When reading streams, the better approach (and it does it in the ziplib
docs) is to iterate under Read returns <=0; Read is only guaranteed to
return *some* data; I'm guessing this is the problem. You can also save on
the memory footprint by only needing a small buffer, rather than the entire
file size...

The following function (and override) correctly copiesbetween two streams;
use it instead.
Oh, and consider "using" the streams so that they get disposed instead of
finalised - but since you are closing them this will only make a (genuine)
difference when exceptions are thrown.

Marc

/// <summary>
/// Read all data from one stream into another, in user-defined chunks
/// </summary>
/// <param name="input">Stream to read from</param>
/// <param name="output">Target stream</param>
/// <param name="bufferSize">Chunk size (bytes)</param>
/// <returns>The total number of bytes copied</returns>
private long CopyStream(Stream input, Stream output, int bufferSize) {
byte[] buffer = new byte[bufferSize];
int bytesRead;
long totalBytes = 0;
while ((bytesRead = input.Read(buffer, 0, bufferSize)) > 0) {
output.Write(buffer, 0, bytesRead);
totalBytes += bytesRead;
}
output.Flush();
return totalBytes;
}

/// <summary>
/// Read all data from one stream into another
/// </summary>
/// <param name="input">Stream to read from</param>
/// <param name="output">Target stream</param>
/// <returns>The total number of bytes copied</returns>
private long CopyStream(Stream input, Stream output) {
return CopyStream(input, output, 1024);
}
May 25 '06 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Chris | last post by:
Hi Where can I find info on unzipping file with VB.NET. I need to unzip a winzip file with my application Thanks
1
by: Jean Christophe Avard | last post by:
Hi! Finally I figure out what was wrong... "objZipEntry.size = strmFile" I wasn't giving the right file length... But now, I have issue with the unzip function... I can't unzip it, the unzip function...
3
by: SDRoy | last post by:
Hello Can someone tell me how I can unzip a .zip file in C#. The zip file is already there and I just need to unzip..not zip and unzip. -- Thanks, SDRoy
4
by: DataSmash | last post by:
I need to unzip all zip file(s) in the current directory into their own subdirectories. The zip file name(s) always start with the string "usa" and end with ".zip". The code below will make the...
0
by: Rocky Zhou | last post by:
python unzip At first, I tried to use 'os.popen3("unzip ...") like this: fin, fout, ferr = os.popen3("unzip -o -d %s %s" % (dest, zipfile)) strerr = ferr.read() # This makes the program hanging...
3
by: sdoty044 | last post by:
I am a true n00b... and I just using Python to complete some very small uneventful task, but need help with one last thing. Basically, this I what I am trying to do. make a temp directory...
5
by: =?Utf-8?B?anVsaW8=?= | last post by:
Hi, I write a program to unzip a Tar file generated on a Unix environment file using SharpZipLib, but returns a error "Header checksum is invalid" when execute the program. This error appears...
1
by: olddocks | last post by:
I want to upload a zip file and then extract/unzip it. I am accomplishing this with php exec command. I am calling unzip from php exec command within a php script and it is not extracting files. why?...
2
by: somsub | last post by:
Hi all, Here is my samle code use strict ; use warnings ; use IO::Uncompress::Unzip ; When I compiled this three lines of code in win32 I got error like below.
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.