fork(1) download
  1. using System;
  2. using System.IO;
  3. using System.IO.Packaging;
  4.  
  5. namespace ZipSample
  6. {
  7. class Program
  8. {
  9. static void Main(string[] args)
  10. {
  11. AddFileToZip("Output.zip", @"C:\Windows\Notepad.exe");
  12. }
  13.  
  14. private const long BUFFER_SIZE = 4096;
  15.  
  16. private static void AddFileToZip(string zipFilename, string fileToAdd)
  17. {
  18. using (Package zip = System.IO.Packaging.Package.Open(zipFilename, FileMode.OpenOrCreate))
  19. {
  20. string destFilename = ".\\" + Path.GetFileName(fileToAdd);
  21. //Uri uri = PackUriHelper.CreatePartUri(new Uri("Notepad.exe", UriKind.Relative));
  22. Uri uri = PackUriHelper.CreatePartUri(new Uri("記事本.exe", UriKind.Relative));
  23.  
  24. if (zip.PartExists(uri))
  25. {
  26. zip.DeletePart(uri);
  27. }
  28. PackagePart part = zip.CreatePart(uri, "", CompressionOption.Normal);
  29. using (FileStream fileStream = new FileStream(fileToAdd, FileMode.Open, FileAccess.Read))
  30. {
  31. using (Stream dest = part.GetStream())
  32. {
  33. CopyStream(fileStream, dest);
  34. }
  35. }
  36. }
  37. }
  38.  
  39. private static void CopyStream(System.IO.FileStream inputStream, System.IO.Stream outputStream)
  40. {
  41. long bufferSize = inputStream.Length < BUFFER_SIZE ? inputStream.Length : BUFFER_SIZE;
  42. byte[] buffer = new byte[bufferSize];
  43. int bytesRead = 0;
  44. long bytesWritten = 0;
  45. while ((bytesRead = inputStream.Read(buffer, 0, buffer.Length)) != 0)
  46. {
  47. outputStream.Write(buffer, 0, bytesRead);
  48. bytesWritten += bytesRead;
  49. }
  50. }
  51. }
  52. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cs(3,17): error CS0234: The type or namespace name `Packaging' does not exist in the namespace `System.IO'. Are you missing an assembly reference?
Compilation failed: 1 error(s), 0 warnings
stdout
Standard output is empty