I have this code on production to save a file to a passworded zip:
ZipOutputStream zipStream = new ZipOutputStream(zipPackage); zipStream.Password = "password"; ZipEntry newEntry = new ZipEntry(Path.GetFileName(pathToFile)); newEntry.DateTime = fi.LastWriteTime; newEntry.Size = fi.Length; zipStream.PutNextEntry(newEntry); byte[ buffer = new byte[4096]; using (System.IO.FileStream streamReader = System.IO.File.OpenRead(pathToFile)) { StreamUtils.Copy(streamReader, zipStream, buffer); } zipStream.CloseEntry();
however the resulting zip file is corrupt if the input file is over (approx.) 20Kb.
Changing the code that does the stream copy to this makes the output zip process correctly:
using (FileStream fs = File.OpenRead(pathToFile)) { byte[ buffer = new byte[fs.Length]; fs.Read(buffer, 0, buffer.Length); newEntry.Size = buffer.Length; // This is very important zipStream.PutNextEntry(newEntry); zipStream.Write(buffer, 0, buffer.Length); } zipStream.CloseEntry();
Is there a problem with the StreamUtils.Copy function or the way I'm calling it please?
Cheers,
John