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 2.3 kB

11 years ago
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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. FileStream _FileStream = new FileStream(fileName, FileMode.Create, FileAccess.Write);
  13. _FileStream.Write(content, 0, content.Length);
  14. _FileStream.Close();
  15. return true;
  16. }
  17. catch (Exception _Exception)
  18. {
  19. Console.WriteLine("Exception caught in process: {0}",
  20. _Exception.ToString());
  21. }
  22. return false;
  23. }
  24. public static void UncompressFile(string fileName, byte[] content)
  25. {
  26. FileStream destinationFile = File.Create(fileName);
  27. // Because the uncompressed size of the file is unknown,
  28. // we are using an arbitrary buffer size.
  29. byte[] buffer = new byte[4096];
  30. int n;
  31. using (GZipStream input = new GZipStream(new MemoryStream(content),
  32. CompressionMode.Decompress, false))
  33. {
  34. while ((n = input.Read(buffer, 0, buffer.Length)) > 0)
  35. {
  36. destinationFile.Write(buffer, 0, n);
  37. }
  38. }
  39. destinationFile.Close();
  40. }
  41. public static void CompressFile(string fileName, byte[] content)
  42. {
  43. FileStream destinationFile = File.Create(fileName);
  44. MemoryStream ms = new MemoryStream(content);
  45. // Because the compressed size of the file is unknown,
  46. // we are using an arbitrary buffer size.
  47. byte[] buffer = new byte[4096];
  48. int n;
  49. using (GZipStream output = new GZipStream(destinationFile,
  50. CompressionMode.Compress, false))
  51. {
  52. while ((n = ms.Read(buffer, 0, buffer.Length)) > 0)
  53. {
  54. output.Write(buffer, 0, n);
  55. }
  56. }
  57. destinationFile.Close();
  58. }
  59. }
  60. }