Beispiel #1
0
 public void PutNextEntry(TarEntry entry)
 {
     if (entry == null)
     {
         throw new ArgumentNullException("entry");
     }
     if (entry.TarHeader.Name.Length >= 100)
     {
         TarHeader header = new TarHeader
         {
             TypeFlag = 0x4c,
             UserId = 0, GroupId = 0,
             GroupName = "",
             UserName = "",
             LinkName = "",
             Size = entry.TarHeader.Name.Length
         };
         header.Name += "././@LongLink";
         header.WriteHeader(blockBuffer);
         buffer.WriteBlock(blockBuffer);
         int nameOffset = 0;
         while (nameOffset < entry.TarHeader.Name.Length)
         {
             Array.Clear(blockBuffer, 0, blockBuffer.Length);
             TarHeader.GetAsciiBytes(entry.TarHeader.Name, nameOffset, blockBuffer, 0, 0x200);
             nameOffset += 0x200;
             buffer.WriteBlock(blockBuffer);
         }
     }
     entry.WriteEntryHeader(blockBuffer);
     buffer.WriteBlock(blockBuffer);
     currBytes = 0L;
     currSize = entry.IsDirectory ? 0L : entry.Size;
 }
Beispiel #2
0
 private void ExtractEntry(string destDir, TarEntry entry)
 {
     int num;
     OnProgressMessageEvent(entry, null);
     string name = entry.Name;
     if (Path.IsPathRooted(name))
     {
         name = name.Substring(Path.GetPathRoot(name).Length);
     }
     name = name.Replace('/', Path.DirectorySeparatorChar);
     string directoryName = Path.Combine(destDir, name);
     if (entry.IsDirectory)
     {
         EnsureDirectoryExists(directoryName);
         return;
     }
     EnsureDirectoryExists(Path.GetDirectoryName(directoryName));
     bool flag = true;
     FileInfo info = new FileInfo(directoryName);
     if (info.Exists)
     {
         if (keepOldFiles)
         {
             OnProgressMessageEvent(entry, "Destination file already exists");
             flag = false;
         }
         else if ((info.Attributes & FileAttributes.ReadOnly) != 0)
         {
             OnProgressMessageEvent(entry, "Destination file already exists, and is read-only");
             flag = false;
         }
     }
     if (!flag)
     {
         return;
     }
     bool flag2 = false;
     Stream stream = File.Create(directoryName);
     if (asciiTranslate)
     {
         flag2 = !IsBinary(directoryName);
     }
     StreamWriter writer = null;
     if (flag2)
     {
         writer = new StreamWriter(stream);
     }
     byte[] buffer = new byte[0x8000];
     Label_00DF:
     num = tarIn.Read(buffer, 0, buffer.Length);
     if (num > 0)
     {
         if (flag2)
         {
             int index = 0;
             for (int i = 0; i < num; i++)
             {
                 if (buffer[i] == 10)
                 {
                     string str4 = Encoding.ASCII.GetString(buffer, index, i - index);
                     writer.WriteLine(str4);
                     index = i + 1;
                 }
             }
         }
         else
         {
             stream.Write(buffer, 0, num);
         }
         goto Label_00DF;
     }
     if (flag2)
     {
         writer.Close();
     }
     else
     {
         stream.Close();
     }
 }
Beispiel #3
0
 private void WriteEntryCore(TarEntry sourceEntry, bool recurse)
 {
     string path = null;
     string file = sourceEntry.File;
     TarEntry entry = (TarEntry)sourceEntry.Clone();
     if (applyUserInfoOverrides)
     {
         entry.GroupId = _groupId;
         entry.GroupName = groupName;
         entry.UserId = _userId;
         entry.UserName = _userName;
     }
     OnProgressMessageEvent(entry, null);
     if ((asciiTranslate && !entry.IsDirectory) && !IsBinary(file))
     {
         path = Path.GetTempFileName();
         using (StreamReader reader = File.OpenText(file))
         {
             using (Stream stream = File.Create(path))
             {
                 Label_0088:
                 string str3 = reader.ReadLine();
                 if (str3 != null)
                 {
                     byte[] bytes = Encoding.ASCII.GetBytes(str3);
                     stream.Write(bytes, 0, bytes.Length);
                     stream.WriteByte(10);
                     goto Label_0088;
                 }
                 stream.Flush();
             }
         }
         entry.Size = new FileInfo(path).Length;
         file = path;
     }
     string str4 = null;
     if ((rootPath != null) && entry.Name.StartsWith(rootPath))
     {
         str4 = entry.Name.Substring(rootPath.Length + 1);
     }
     if (pathPrefix != null)
     {
         str4 = (str4 == null) ? (pathPrefix + "/" + entry.Name) : (pathPrefix + "/" + str4);
     }
     if (str4 != null)
     {
         entry.Name = str4;
     }
     tarOut.PutNextEntry(entry);
     if (entry.IsDirectory)
     {
         if (recurse)
         {
             TarEntry[] directoryEntries = entry.GetDirectoryEntries();
             for (int i = 0; i < directoryEntries.Length; i++)
             {
                 WriteEntryCore(directoryEntries[i], true);
             }
             return;
         }
         return;
     }
     using (Stream stream2 = File.OpenRead(file))
     {
         byte[] buffer = new byte[0x8000];
         while (true)
         {
             int count = stream2.Read(buffer, 0, buffer.Length);
             if (count <= 0)
             {
                 goto Label_01F6;
             }
             tarOut.Write(buffer, 0, count);
         }
     }
     Label_01F6:
     if (!string.IsNullOrEmpty(path))
     {
         File.Delete(path);
     }
     tarOut.CloseEntry();
 }
Beispiel #4
0
 public void WriteEntry(TarEntry sourceEntry, bool recurse)
 {
     if (sourceEntry == null)
     {
         throw new ArgumentNullException("sourceEntry");
     }
     if (isDisposed)
     {
         throw new ObjectDisposedException("TarArchive");
     }
     try
     {
         if (recurse)
         {
             TarHeader.SetValueDefaults(sourceEntry.UserId, sourceEntry.UserName, sourceEntry.GroupId, sourceEntry.GroupName);
         }
         WriteEntryCore(sourceEntry, recurse);
     }
     finally
     {
         if (recurse)
         {
             TarHeader.RestoreSetValues();
         }
     }
 }
Beispiel #5
0
 protected virtual void OnProgressMessageEvent(TarEntry entry, string message)
 {
     ProgressMessageHandler progressMessageEvent = ProgressMessageEvent;
     if (progressMessageEvent != null)
     {
         progressMessageEvent(this, entry, message);
     }
 }
Beispiel #6
0
 public bool IsDescendent(TarEntry toTest)
 {
     if (toTest == null)
     {
         throw new ArgumentNullException("toTest");
     }
     return toTest.Name.StartsWith(Name);
 }
Beispiel #7
0
 public TarEntry[] GetDirectoryEntries()
 {
     if ((file == null) || !Directory.Exists(file))
     {
         return new TarEntry[0];
     }
     string[] fileSystemEntries = Directory.GetFileSystemEntries(file);
     TarEntry[] entryArray = new TarEntry[fileSystemEntries.Length];
     for (int i = 0; i < fileSystemEntries.Length; i++)
     {
         entryArray[i] = CreateEntryFromFile(fileSystemEntries[i]);
     }
     return entryArray;
 }
Beispiel #8
0
 public static TarEntry CreateTarEntry(string name)
 {
     TarEntry entry = new TarEntry();
     NameTarHeader(entry.header, name);
     return entry;
 }
Beispiel #9
0
 public static TarEntry CreateEntryFromFile(string fileName)
 {
     TarEntry entry = new TarEntry();
     entry.GetFileTarHeader(entry.header, fileName);
     return entry;
 }
Beispiel #10
0
 public TarEntry GetNextEntry()
 {
     if (hasHitEOF)
     {
         return null;
     }
     if (currentEntry != null)
     {
         SkipToNextEntry();
     }
     byte[] block = tarBuffer.ReadBlock();
     if (block == null)
     {
         hasHitEOF = true;
     }
     else if (TarBuffer.IsEndOfArchiveBlock(block))
     {
         hasHitEOF = true;
     }
     if (hasHitEOF)
     {
         currentEntry = null;
     }
     else
     {
         try
         {
             TarHeader header = new TarHeader();
             header.ParseBuffer(block);
             if (!header.IsChecksumValid)
             {
                 throw new TarException("Header checksum is invalid");
             }
             entryOffset = 0L;
             entrySize = header.Size;
             StringBuilder builder = null;
             if (header.TypeFlag == 0x4c)
             {
                 byte[] buffer = new byte[0x200];
                 long size = entrySize;
                 builder = new StringBuilder();
                 while (size > 0L)
                 {
                     int length = Read(buffer, 0, (size > buffer.Length) ? buffer.Length : ((int)size));
                     if (length == -1)
                     {
                         throw new InvalidHeaderException("Failed to read long name entry");
                     }
                     builder.Append(TarHeader.ParseName(buffer, 0, length));
                     size -= length;
                 }
                 SkipToNextEntry();
                 block = tarBuffer.ReadBlock();
             }
             else if (header.TypeFlag == 0x67)
             {
                 SkipToNextEntry();
                 block = tarBuffer.ReadBlock();
             }
             else if (header.TypeFlag == 120)
             {
                 SkipToNextEntry();
                 block = tarBuffer.ReadBlock();
             }
             else if (header.TypeFlag == 0x56)
             {
                 SkipToNextEntry();
                 block = tarBuffer.ReadBlock();
             }
             else if (((header.TypeFlag != 0x30) && (header.TypeFlag != 0)) && (header.TypeFlag != 0x35))
             {
                 SkipToNextEntry();
                 block = tarBuffer.ReadBlock();
             }
             if (entryFactory == null)
             {
                 currentEntry = new TarEntry(block);
                 if (builder != null)
                 {
                     currentEntry.Name = builder.ToString();
                 }
             }
             else
             {
                 currentEntry = entryFactory.CreateEntry(block);
             }
             entryOffset = 0L;
             entrySize = currentEntry.Size;
         }
         catch (InvalidHeaderException exception)
         {
             entrySize = 0L;
             entryOffset = 0L;
             currentEntry = null;
             throw new InvalidHeaderException(string.Format("Bad header in record {0} block {1} {2}", tarBuffer.CurrentRecord, tarBuffer.CurrentBlock, exception.Message));
         }
     }
     return currentEntry;
 }