All pastes #814802 Raw Edit

KeeperOfTheSoul

public text v1 · immutable
·published 2007-12-13 15:49 UTC
rendered paste body
private void Example(FileEntry[] files, Stream stream) {
 ICSharpCode.SharpZipLib.Zip.ZipOutputStream outputStream;
 ICSharpCode.SharpZipLib.Zip.ZipEntry zipEntry;
 FileStream inputStream;
 FileInfo info;
 
 outputStream = new ICSharpCode.SharpZipLib.Zip.ZipOutputStream(stream);
 outputStream.IsStreamOwner = False;
 outputStream.SetLevel(5);

 for (int i = 0; i < files.Length; i++) {
  info = new FileInfo(files[i].FullPath);
  zipEntry = new ICSharpCode.SharpZipLib.Zip.ZipEntry(files[i].Name);
  zipEntry.CompressionMethod = ICSharpCode.SharpZipLib.Zip.CompressionMethod.Deflated;
  zipEntry.DateTime = info.LastWriteTimeUtc;
  zipEntry.Size = info.Length;
  
  outputStream.PutNextEntry(zipEntry);
  
  using (inputStream = new FileStream(info.FullName, FileMode.Open, FileAccess.Read, FileShare.Read, 16384) {
   CopyStream(inputStream, outputStream, 16384);
  }
  
  outputStream.CloseEntry();
 }
 

 outputStream.Finish();
 outputStream.Flush();
 outputStream.Close();
}

public static void CopyStream(Stream src, Stream dst, int bufsize)
{
 byte[ buffer = new byte[bufsize];
 while(true) {
  int count = src.Read(buffer, 0, bufsize);
  if (count <= 0) return;  
  dst.Write(buffer, 0, count);
 }
}

private struct FileEntry {
 public string FullPath; //path to the physical file to store
 public string Name;     //name of the file once stored
}