2using System.Collections.Generic;
 
    8using System.Threading.Tasks;
 
   38        public static int ExtractTar(
string strFileName, 
string strOutputDir, 
CancelEvent evtCancel = 
null, 
Log log = 
null, 
int nExpectedTotal = 0, 
int nIdx = 0)
 
   40            using (FileStream fstrm = File.OpenRead(strFileName))
 
   42                return ExtractTar(fstrm, strOutputDir, evtCancel, log, nExpectedTotal, nIdx);
 
   56        public static int ExtractTar(Stream stream, 
string strOutputDir, 
CancelEvent evtCancel = 
null, 
Log log = 
null, 
int nExpectedTotal = 0, 
int nIdx = 0)
 
   58            byte[] rgBuffer = 
new byte[500];
 
   61            Stopwatch sw = 
new Stopwatch();
 
   69                    stream.Read(rgBuffer, 0, 100);
 
   70                    string strName = Encoding.ASCII.GetString(rgBuffer).Trim(
'\0');
 
   72                    if (
string.IsNullOrWhiteSpace(strName))
 
   75                    stream.Seek(24, SeekOrigin.Current);
 
   76                    stream.Read(rgBuffer, 0, 12);
 
   78                    long lSize = Convert.ToInt64(Encoding.UTF8.GetString(rgBuffer, 0, 12).Trim(
'\0').Trim(), 8);
 
   80                    stream.Seek(376L, SeekOrigin.Current);
 
   82                    string strOutput = Path.Combine(strOutputDir, strName);
 
   83                    string strPath = Path.GetDirectoryName(strOutput);
 
   85                    if (!Directory.Exists(strPath))
 
   86                        Directory.CreateDirectory(strPath);
 
   88                    if (!strName.EndsWith(
"/") && !strName.EndsWith(
"\\"))
 
   90                        if (sw.Elapsed.TotalMilliseconds > 1000)
 
   94                                if (nExpectedTotal > 0)
 
   95                                    log.Progress = (double)(nIdx + nFileCount) / nExpectedTotal;
 
   97                                log.WriteLine(
"Extracting " + nFileCount.ToString(
"N0") + 
" files - '" + strName + 
"'...");
 
  103                        using (FileStream fstrm = File.Open(strOutput, FileMode.OpenOrCreate, FileAccess.Write))
 
  105                            byte[] rgData = 
new byte[lSize];
 
  106                            stream.Read(rgData, 0, rgData.Length);
 
  107                            fstrm.Write(rgData, 0, rgData.Length);
 
  112                    long lPos = stream.Position;
 
  113                    long lOffset = 512 - (lPos % 512);
 
  117                    stream.Seek(lOffset, SeekOrigin.Current);
 
  119                    if (evtCancel != 
null)
 
  121                        if (evtCancel.WaitOne(0))
 
  127                    log.WriteLine(
"Extracted a total of " + nFileCount.ToString(
"N0") + 
" files.");
 
  129            catch (Exception excpt)
 
  132                    log.WriteError(excpt);
 
  136            return nFileCount + nIdx;
 
  144        public static void ExtractTarGz(
string strFileName, 
string strOutputDir)
 
  146            using (FileStream fstrm = File.OpenRead(strFileName))
 
  160            using (Stream gzip = 
new GZipStream(stream, CompressionMode.Decompress))
 
  162                const int nChunk = 4096;
 
  164                using (MemoryStream ms = 
new MemoryStream())
 
  166                    byte[] rgBuffer = 
new byte[nChunk];
 
  167                    int nRead = gzip.Read(rgBuffer, 0, nChunk);
 
  171                        ms.Write(rgBuffer, 0, nRead);
 
  172                        nRead = gzip.Read(rgBuffer, 0, nChunk);
 
  175                    ms.Seek(0, SeekOrigin.Begin);
 
The CancelEvent provides an extension to the manual cancel event that allows for overriding the manua...
 
The Log class provides general output in text form.
 
The TarFile functions are used to expand tar files.
 
static void ExtractTarGz(string strFileName, string strOutputDir)
Extract a Gz zipped file to the output directory.
 
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.
 
TarFile()
The constructor.
 
static void ExtractTarGz(Stream stream, string strOutputDir)
Extract a Gz stream to the output directory.
 
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.
 
The MyCaffe.basecode contains all generic types used throughout MyCaffe.