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.8 kB

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