Guest User

Untitled

a guest
Jun 20th, 2018
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.88 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using java.util;
  5. using java.util.zip;
  6. using java.io;
  7. using System.IO;
  8.  
  9. namespace cZip
  10. {
  11. public class cZip
  12. {
  13. public static void Zip(string zipFileName, List<string> sourceFiles)
  14. {
  15. FileOutputStream filOpStrm = new FileOutputStream(zipFileName);
  16. ZipOutputStream zipOpStrm = new ZipOutputStream(filOpStrm);
  17. FileInputStream filIpStrm = null;
  18. foreach (string strFilName in sourceFiles)
  19. {
  20. filIpStrm = new FileInputStream(strFilName);
  21. ZipEntry ze = new ZipEntry(Path.GetFileName(strFilName));
  22. zipOpStrm.putNextEntry(ze);
  23. sbyte[] buffer = new sbyte[1024];
  24. int len = 0;
  25. while ((len =
  26.  
  27. filIpStrm.read(buffer)) >= 0)
  28. {
  29. zipOpStrm.write(buffer, 0, len);
  30. }
  31. }
  32. zipOpStrm.closeEntry();
  33. filIpStrm.close();
  34. zipOpStrm.close();
  35. filOpStrm.close();
  36. }
  37. public static void Extract(string zipFileName, string destinationPath)
  38. {
  39. ZipFile zipfile = new ZipFile(zipFileName);
  40. List<ZipEntry> zipFiles = GetZipFiles(zipfile);
  41.  
  42. foreach (ZipEntry zipFile in zipFiles)
  43. {
  44. if (!zipFile.isDirectory())
  45. {
  46. InputStream s = zipfile.getInputStream(zipFile);
  47. try
  48. {
  49. Directory.CreateDirectory(String.Format(@"{0}{1}", destinationPath, Path.GetDirectoryName(zipFile.getName())));
  50. FileOutputStream dest = new FileOutputStream(Path.Combine(destinationPath + "\" + Path.GetDirectoryName(zipFile.getName()), Path.GetFileName(zipFile.getName())));
  51. try
  52. {
  53. int len = 0;
  54. sbyte[] buffer = new sbyte[7168];
  55. while ((len = s.read(buffer)) >= 0)
  56. {
  57. dest.write(buffer, 0, len);
  58. }
  59. }
  60. finally
  61. {
  62. dest.close();
  63. }
  64. }
  65. finally
  66. {
  67. s.close();
  68. }
  69. }
  70. }
  71. }
  72. public static List<ZipEntry> GetZipFiles(ZipFile zipfil)
  73. {
  74. List<ZipEntry> lstZip = new List<ZipEntry>();
  75. Enumeration zipEnum = zipfil.entries();
  76. while (zipEnum.hasMoreElements())
  77. {
  78. ZipEntry zip = (ZipEntry)zipEnum.nextElement();
  79. lstZip.Add(zip);
  80. }
  81. return lstZip;
  82. }
  83. }
  84.  
  85. }
Add Comment
Please, Sign In to add comment