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.

Compress.cs 4.0 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. /*****************************************************************************
  2. Copyright 2018 The TensorFlow.NET Authors. All Rights Reserved.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. ******************************************************************************/
  13. using ICSharpCode.SharpZipLib.Core;
  14. using ICSharpCode.SharpZipLib.GZip;
  15. using ICSharpCode.SharpZipLib.Tar;
  16. using System;
  17. using System.IO;
  18. using System.IO.Compression;
  19. using System.Linq;
  20. using System.Threading;
  21. using System.Threading.Tasks;
  22. namespace Tensorflow.Keras.Utils
  23. {
  24. public class Compress
  25. {
  26. public static void ExtractGZip(string gzipFileName, string targetDir)
  27. {
  28. // Use a 4K buffer. Any larger is a waste.
  29. byte[] dataBuffer = new byte[4096];
  30. using (System.IO.Stream fs = new FileStream(gzipFileName, FileMode.Open, FileAccess.Read))
  31. {
  32. using (GZipInputStream gzipStream = new GZipInputStream(fs))
  33. {
  34. // Change this to your needs
  35. string fnOut = Path.Combine(targetDir, Path.GetFileNameWithoutExtension(gzipFileName));
  36. using (FileStream fsOut = File.Create(fnOut))
  37. {
  38. StreamUtils.Copy(gzipStream, fsOut, dataBuffer);
  39. }
  40. }
  41. }
  42. }
  43. public static void UnZip(String gzArchiveName, String destFolder)
  44. {
  45. var flag = gzArchiveName.Split(Path.DirectorySeparatorChar).Last().Split('.').First() + ".bin";
  46. if (File.Exists(Path.Combine(destFolder, flag))) return;
  47. var destFileName = gzArchiveName.Replace(".zip", string.Empty);
  48. if (File.Exists(destFileName)) return;
  49. Binding.tf_output_redirect.WriteLine($"Extracting.");
  50. var task = Task.Run(() =>
  51. {
  52. ZipFile.ExtractToDirectory(gzArchiveName, destFolder);
  53. });
  54. while (!task.IsCompleted)
  55. {
  56. Thread.Sleep(200);
  57. Binding.tf_output_redirect.Write(".");
  58. }
  59. File.Create(Path.Combine(destFolder, flag));
  60. Binding.tf_output_redirect.WriteLine("");
  61. Binding.tf_output_redirect.WriteLine("Extracting is completed.");
  62. }
  63. public static void ExtractTGZ(String gzArchiveName, String destFolder)
  64. {
  65. var flag = gzArchiveName.Split(Path.DirectorySeparatorChar).Last().Split('.').First() + ".bin";
  66. if (File.Exists(Path.Combine(destFolder, flag))) return;
  67. Binding.tf_output_redirect.WriteLine($"Extracting.");
  68. var task = Task.Run(() =>
  69. {
  70. using (var inStream = File.OpenRead(gzArchiveName))
  71. {
  72. using (var gzipStream = new GZipInputStream(inStream))
  73. {
  74. using (TarArchive tarArchive = TarArchive.CreateInputTarArchive(gzipStream))
  75. tarArchive.ExtractContents(destFolder);
  76. }
  77. }
  78. });
  79. while (!task.IsCompleted)
  80. {
  81. Thread.Sleep(200);
  82. Binding.tf_output_redirect.Write(".");
  83. }
  84. File.Create(Path.Combine(destFolder, flag));
  85. Binding.tf_output_redirect.WriteLine("");
  86. Binding.tf_output_redirect.WriteLine("Extracting is completed.");
  87. }
  88. }
  89. }