ZipFileController.cs 1.0 KB

123456789101112131415161718192021222324252627282930
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using System.IO.Compression;
  5. using System.IO;
  6. using System;
  7. public class ZipFileController
  8. {
  9. // Start is called before the first frame update
  10. public static void ZipFile(string inputFile, string outputFile)
  11. {
  12. // 要压缩的文件路径
  13. string fileToZip = inputFile;
  14. // 压缩后的 ZIP 文件路径
  15. string zipFilePath = outputFile;
  16. // 创建一个新的 ZIP 文件并将文件添加到其中
  17. using (FileStream zipToOpen = new FileStream(zipFilePath, FileMode.Create))
  18. {
  19. using (ZipArchive archive = new ZipArchive(zipToOpen, ZipArchiveMode.Create))
  20. {
  21. // 获取文件名
  22. string fileName = Path.GetFileName(fileToZip);
  23. // 将文件添加到 ZIP 文件中
  24. archive.CreateEntryFromFile(fileToZip, fileName);
  25. }
  26. }
  27. Console.WriteLine("文件已成功压缩到: " + zipFilePath);
  28. }
  29. }