You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

FileManager.cs 1.4 kB

10 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. using System;
  2. using System.IO;
  3. using System.IO.Compression;
  4. namespace Shadowsocks.Controller
  5. {
  6. public class FileManager
  7. {
  8. public static bool ByteArrayToFile(string fileName, byte[] content)
  9. {
  10. try
  11. {
  12. using (var fs = new FileStream(fileName, FileMode.Create, FileAccess.Write))
  13. fs.Write(content, 0, content.Length);
  14. return true;
  15. }
  16. catch (Exception ex)
  17. {
  18. Console.WriteLine("Exception caught in process: {0}",
  19. ex.ToString());
  20. }
  21. return false;
  22. }
  23. public static void UncompressFile(string fileName, byte[] content)
  24. {
  25. // Because the uncompressed size of the file is unknown,
  26. // we are using an arbitrary buffer size.
  27. byte[] buffer = new byte[4096];
  28. int n;
  29. using(var fs = File.Create(fileName))
  30. using (var input = new GZipStream(
  31. new MemoryStream(content),
  32. CompressionMode.Decompress, false))
  33. {
  34. while ((n = input.Read(buffer, 0, buffer.Length)) > 0)
  35. {
  36. fs.Write(buffer, 0, n);
  37. }
  38. }
  39. }
  40. }
  41. }