MyCaffe  1.12.2.41
Deep learning software for Windows C# programmers.
TarFile.cs
1using System;
2using System.Collections.Generic;
3using System.Diagnostics;
4using System.IO;
5using System.IO.Compression;
6using System.Linq;
7using System.Text;
8using System.Threading.Tasks;
9
10namespace MyCaffe.basecode
11{
19 public class TarFile
20 {
24 public TarFile()
25 {
26 }
27
38 public static int ExtractTar(string strFileName, string strOutputDir, CancelEvent evtCancel = null, Log log = null, int nExpectedTotal = 0, int nIdx = 0)
39 {
40 using (FileStream fstrm = File.OpenRead(strFileName))
41 {
42 return ExtractTar(fstrm, strOutputDir, evtCancel, log, nExpectedTotal, nIdx);
43 }
44 }
45
56 public static int ExtractTar(Stream stream, string strOutputDir, CancelEvent evtCancel = null, Log log = null, int nExpectedTotal = 0, int nIdx = 0)
57 {
58 byte[] rgBuffer = new byte[500];
59 bool bDone = false;
60 int nFileCount = 0;
61 Stopwatch sw = new Stopwatch();
62
63 sw.Start();
64
65 try
66 {
67 while (!bDone)
68 {
69 stream.Read(rgBuffer, 0, 100);
70 string strName = Encoding.ASCII.GetString(rgBuffer).Trim('\0');
71
72 if (string.IsNullOrWhiteSpace(strName))
73 break;
74
75 stream.Seek(24, SeekOrigin.Current);
76 stream.Read(rgBuffer, 0, 12);
77
78 long lSize = Convert.ToInt64(Encoding.UTF8.GetString(rgBuffer, 0, 12).Trim('\0').Trim(), 8);
79
80 stream.Seek(376L, SeekOrigin.Current);
81
82 string strOutput = Path.Combine(strOutputDir, strName);
83 string strPath = Path.GetDirectoryName(strOutput);
84
85 if (!Directory.Exists(strPath))
86 Directory.CreateDirectory(strPath);
87
88 if (!strName.EndsWith("/") && !strName.EndsWith("\\"))
89 {
90 if (sw.Elapsed.TotalMilliseconds > 1000)
91 {
92 if (log != null)
93 {
94 if (nExpectedTotal > 0)
95 log.Progress = (double)(nIdx + nFileCount) / nExpectedTotal;
96
97 log.WriteLine("Extracting " + nFileCount.ToString("N0") + " files - '" + strName + "'...");
98 }
99
100 sw.Restart();
101 }
102
103 using (FileStream fstrm = File.Open(strOutput, FileMode.OpenOrCreate, FileAccess.Write))
104 {
105 byte[] rgData = new byte[lSize];
106 stream.Read(rgData, 0, rgData.Length);
107 fstrm.Write(rgData, 0, rgData.Length);
108 nFileCount++;
109 }
110 }
111
112 long lPos = stream.Position;
113 long lOffset = 512 - (lPos % 512);
114 if (lOffset == 512)
115 lOffset = 0;
116
117 stream.Seek(lOffset, SeekOrigin.Current);
118
119 if (evtCancel != null)
120 {
121 if (evtCancel.WaitOne(0))
122 return 0;
123 }
124 }
125
126 if (log != null)
127 log.WriteLine("Extracted a total of " + nFileCount.ToString("N0") + " files.");
128 }
129 catch (Exception excpt)
130 {
131 if (log != null)
132 log.WriteError(excpt);
133 throw excpt;
134 }
135
136 return nFileCount + nIdx;
137 }
138
144 public static void ExtractTarGz(string strFileName, string strOutputDir)
145 {
146 using (FileStream fstrm = File.OpenRead(strFileName))
147 {
148 ExtractTarGz(fstrm, strOutputDir);
149 }
150 }
151
157 public static void ExtractTarGz(Stream stream, string strOutputDir)
158 {
159 // A GZipStream is not seekable, so copy it first to a MemoryStream.
160 using (Stream gzip = new GZipStream(stream, CompressionMode.Decompress))
161 {
162 const int nChunk = 4096;
163
164 using (MemoryStream ms = new MemoryStream())
165 {
166 byte[] rgBuffer = new byte[nChunk];
167 int nRead = gzip.Read(rgBuffer, 0, nChunk);
168
169 while (nRead > 0)
170 {
171 ms.Write(rgBuffer, 0, nRead);
172 nRead = gzip.Read(rgBuffer, 0, nChunk);
173 }
174
175 ms.Seek(0, SeekOrigin.Begin);
176 ExtractTar(ms, strOutputDir);
177 }
178 }
179 }
180 }
181}
The CancelEvent provides an extension to the manual cancel event that allows for overriding the manua...
Definition: CancelEvent.cs:17
The Log class provides general output in text form.
Definition: Log.cs:13
The TarFile functions are used to expand tar files.
Definition: TarFile.cs:20
static void ExtractTarGz(string strFileName, string strOutputDir)
Extract a Gz zipped file to the output directory.
Definition: TarFile.cs:144
static int ExtractTar(string strFileName, string strOutputDir, CancelEvent evtCancel=null, Log log=null, int nExpectedTotal=0, int nIdx=0)
Extract a Tar (*.tar) file to a specified output directory.
Definition: TarFile.cs:38
TarFile()
The constructor.
Definition: TarFile.cs:24
static void ExtractTarGz(Stream stream, string strOutputDir)
Extract a Gz stream to the output directory.
Definition: TarFile.cs:157
static int ExtractTar(Stream stream, string strOutputDir, CancelEvent evtCancel=null, Log log=null, int nExpectedTotal=0, int nIdx=0)
Extract Tar data from a stream to a specified output directory.
Definition: TarFile.cs:56
The MyCaffe.basecode contains all generic types used throughout MyCaffe.
Definition: Annotation.cs:12