FileCompressTool.cs 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. //************************************************************************
  2. // https://github.com/yuzhengyang
  3. // author: yuzhengyang
  4. // date: 2017.6.12 - 2017.6.12
  5. // desc: 文件压缩
  6. // Copyright (c) yuzhengyang. All rights reserved.
  7. //************************************************************************
  8. using System;
  9. using System.Collections.Generic;
  10. using System.IO;
  11. using System.IO.Compression;
  12. using System.Linq;
  13. using System.Text;
  14. namespace Y.Utils.IOUtils.FileUtils
  15. {
  16. /// <summary>
  17. /// 文件压缩
  18. /// </summary>
  19. public class FileCompressTool
  20. {
  21. /// <summary>
  22. /// 压缩
  23. /// </summary>
  24. /// <param name="file"></param>
  25. /// <returns></returns>
  26. public static bool Compress(string file)
  27. {
  28. FileInfo fileInfo = null;
  29. if (File.Exists(file)) fileInfo = new FileInfo(file);
  30. if (fileInfo == null) return false;
  31. using (FileStream originalFileStream = fileInfo.OpenRead())
  32. {
  33. if ((File.GetAttributes(fileInfo.FullName) & FileAttributes.Hidden) != FileAttributes.Hidden & fileInfo.Extension != ".gz")
  34. {
  35. using (FileStream compressedFileStream = File.Create(fileInfo.FullName + ".gz"))
  36. {
  37. using (GZipStream compressionStream = new GZipStream(compressedFileStream, CompressionMode.Compress))
  38. {
  39. originalFileStream.CopyTo(compressionStream);
  40. return true;
  41. }
  42. }
  43. }
  44. }
  45. return false;
  46. }
  47. /// <summary>
  48. /// 解压缩
  49. /// </summary>
  50. /// <param name="file"></param>
  51. /// <returns></returns>
  52. public static bool Decompress(string file)
  53. {
  54. FileInfo fileInfo = null;
  55. if (File.Exists(file)) fileInfo = new FileInfo(file);
  56. if (fileInfo == null) return false;
  57. try
  58. {
  59. using (FileStream originalFileStream = fileInfo.OpenRead())
  60. {
  61. string currentFileName = fileInfo.FullName;
  62. string newFileName = currentFileName.Remove(currentFileName.Length - fileInfo.Extension.Length);
  63. using (FileStream decompressedFileStream = File.Create(newFileName))
  64. {
  65. using (GZipStream decompressionStream = new GZipStream(originalFileStream, CompressionMode.Decompress))
  66. {
  67. decompressionStream.CopyTo(decompressedFileStream);
  68. return true;
  69. }
  70. }
  71. }
  72. }
  73. catch (Exception e)
  74. {
  75. return false;
  76. }
  77. }
  78. /// <summary>
  79. /// 单文件压缩(生成的压缩包和第三方的解压软件兼容)
  80. /// </summary>
  81. /// <param name="sourceFilePath"></param>
  82. /// <returns></returns>
  83. public string CompressSingle(string sourceFilePath)
  84. {
  85. string zipFileName = sourceFilePath + ".gz";
  86. using (FileStream sourceFileStream = new FileInfo(sourceFilePath).OpenRead())
  87. {
  88. using (FileStream zipFileStream = File.Create(zipFileName))
  89. {
  90. using (GZipStream zipStream = new GZipStream(zipFileStream, CompressionMode.Compress))
  91. {
  92. sourceFileStream.CopyTo(zipStream);
  93. }
  94. }
  95. }
  96. return zipFileName;
  97. }
  98. /// <summary>
  99. /// 自定义多文件压缩(生成的压缩包和第三方的压缩文件解压不兼容)
  100. /// </summary>
  101. /// <param name="sourceFileList">文件列表</param>
  102. /// <param name="saveFullPath">压缩包全路径</param>
  103. public void CompressMulti(string[] sourceFileList, string saveFullPath)
  104. {
  105. MemoryStream ms = new MemoryStream();
  106. foreach (string filePath in sourceFileList)
  107. {
  108. Console.WriteLine(filePath);
  109. if (File.Exists(filePath))
  110. {
  111. string fileName = Path.GetFileName(filePath);
  112. byte[] fileNameBytes = Encoding.UTF8.GetBytes(fileName);
  113. byte[] sizeBytes = BitConverter.GetBytes(fileNameBytes.Length);
  114. ms.Write(sizeBytes, 0, sizeBytes.Length);
  115. ms.Write(fileNameBytes, 0, fileNameBytes.Length);
  116. byte[] fileContentBytes = File.ReadAllBytes(filePath);
  117. ms.Write(BitConverter.GetBytes(fileContentBytes.Length), 0, 4);
  118. ms.Write(fileContentBytes, 0, fileContentBytes.Length);
  119. }
  120. }
  121. ms.Flush();
  122. ms.Position = 0;
  123. using (FileStream zipFileStream = File.Create(saveFullPath))
  124. {
  125. using (GZipStream zipStream = new GZipStream(zipFileStream, CompressionMode.Compress))
  126. {
  127. ms.Position = 0;
  128. ms.CopyTo(zipStream);
  129. }
  130. }
  131. ms.Close();
  132. }
  133. /// <summary>
  134. /// 多文件压缩解压
  135. /// </summary>
  136. /// <param name="zipPath">压缩文件路径</param>
  137. /// <param name="targetPath">解压目录</param>
  138. public void DeCompressMulti(string zipPath, string targetPath)
  139. {
  140. byte[] fileSize = new byte[4];
  141. if (File.Exists(zipPath))
  142. {
  143. using (FileStream fStream = File.Open(zipPath, FileMode.Open))
  144. {
  145. using (MemoryStream ms = new MemoryStream())
  146. {
  147. using (GZipStream zipStream = new GZipStream(fStream, CompressionMode.Decompress))
  148. {
  149. zipStream.CopyTo(ms);
  150. }
  151. ms.Position = 0;
  152. while (ms.Position != ms.Length)
  153. {
  154. ms.Read(fileSize, 0, fileSize.Length);
  155. int fileNameLength = BitConverter.ToInt32(fileSize, 0);
  156. byte[] fileNameBytes = new byte[fileNameLength];
  157. ms.Read(fileNameBytes, 0, fileNameBytes.Length);
  158. string fileName = System.Text.Encoding.UTF8.GetString(fileNameBytes);
  159. string fileFulleName = targetPath + fileName;
  160. ms.Read(fileSize, 0, 4);
  161. int fileContentLength = BitConverter.ToInt32(fileSize, 0);
  162. byte[] fileContentBytes = new byte[fileContentLength];
  163. ms.Read(fileContentBytes, 0, fileContentBytes.Length);
  164. using (FileStream childFileStream = File.Create(fileFulleName))
  165. {
  166. childFileStream.Write(fileContentBytes, 0, fileContentBytes.Length);
  167. }
  168. }
  169. }
  170. }
  171. }
  172. }
  173. }
  174. }