FileCompressTool.cs 7.2 KB

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