private void ExtractEntry(string destDir, TarEntry entry, ICSharpCode.SharpZipLib.Tar.TarInputStream stream) { string name = entry.Name; if (Path.IsPathRooted(name)) { name = name.Substring(Path.GetPathRoot(name).Length); } name = name.Replace('/', Path.DirectorySeparatorChar); name = name.Substring(name.IndexOf(Path.DirectorySeparatorChar) + 1); string dest = Path.Combine(destDir, name); if (entry.IsDirectory) { Directory.CreateDirectory(dest); } else { Directory.CreateDirectory(Path.GetDirectoryName(dest)); using (Stream outputStream = File.Create(dest)) { stream.CopyEntryContents(outputStream); } } }
static Resources() { using ( MemoryStream memStream = new MemoryStream( FFTPatcher.TextEditor.Properties.Resources.Resources_tar, false ) ) using ( GZipInputStream gzStream = new GZipInputStream( memStream ) ) using ( TarInputStream tarStream = new TarInputStream( gzStream ) ) { TarEntry entry; entry = tarStream.GetNextEntry(); while ( entry != null ) { if ( entry.Size != 0 && !string.IsNullOrEmpty( entry.Name ) ) { if (entry.Name.EndsWith( ".xml" )) { XmlDocument doc = new XmlDocument(); doc.Load( tarStream ); resourceMapping[entry.Name] = doc; } else { byte[] bytes = new byte[entry.Size]; ICSharpCode.SharpZipLib.Core.StreamUtils.ReadFully( tarStream, bytes ); otherResources[entry.Name] = bytes; } } entry = tarStream.GetNextEntry(); } } }
/// <summary> /// Adds a layer around a stream that isolates the <code>data.tar.gz</code> file from a TAR stream. /// </summary> /// <param name="stream">The TAR stream.</param> /// <returns>A stream representing the <code>data.tar.gz</code> data.</returns> /// <exception cref="IOException">The compressed stream contains invalid data.</exception> private static Stream GetPartialStream(Stream stream) { try { var tar = new TarInputStream(stream); while (true) { var entry = tar.GetNextEntry(); if (entry == null) throw new IOException(Resources.RubyGemInvalid); if (entry.Name == "data.tar.gz") return tar; } } #region Error handling catch (SharpZipBaseException ex) { // Wrap exception since only certain exception types are allowed throw new IOException(Resources.ArchiveInvalid, ex); } catch (InvalidDataException ex) { // Wrap exception since only certain exception types are allowed throw new IOException(Resources.ArchiveInvalid, ex); } catch (ArgumentOutOfRangeException ex) { // Wrap exception since only certain exception types are allowed throw new IOException(Resources.ArchiveInvalid, ex); } #endregion }
protected TarArchive(TarInputStream stream) { if ( stream == null ) { throw new ArgumentNullException("stream"); } tarIn = stream; }
public override bool Execute() { if (!NeedsUpdate()) { return(true); } try { var ms = new MemoryStream(); var downloadStream = new System.Net.WebClient().OpenRead(Url); downloadStream.CopyTo(ms); ms.Seek(0, SeekOrigin.Begin); var hash = new SHA256Managed().ComputeHash(ms); if (BitConverter.ToString(hash).Replace("-", "").ToLower() != this.Hash) { Log.LogError("Got wrong hash for {0}", Url); return(false); } try { Directory.Delete(Root, true); } catch (DirectoryNotFoundException) { // Obviously not an issue } ms.Seek(0, SeekOrigin.Begin); var bzStream = new ICSharpCode.SharpZipLib.BZip2.BZip2InputStream(ms); var tarStream = new ICSharpCode.SharpZipLib.Tar.TarInputStream(bzStream); while (true) { TarEntry entry = tarStream.GetNextEntry(); if (entry == null) { break; } ExtractEntry(Root, entry, tarStream); } File.WriteAllText(Path.Combine(Root, "aegisub.hash"), Hash); return(true); } catch (Exception e) { Log.LogErrorFromException(e); return(false); } }
private static Stopwatch ParseDisks(Action<Disk> addToBatch) { int i = 0; var parser = new Parser(); var buffer = new byte[1024*1024];// more than big enough for all files var sp = Stopwatch.StartNew(); using (var bz2 = new BZip2InputStream(File.Open(@"D:\Data\freedb-complete-20120101.tar.bz2", FileMode.Open))) using (var tar = new TarInputStream(bz2)) { TarEntry entry; while((entry=tar.GetNextEntry()) != null) { if(entry.Size == 0 || entry.Name == "README" || entry.Name == "COPYING") continue; var readSoFar = 0; while(true) { var read = tar.Read(buffer, readSoFar, ((int) entry.Size) - readSoFar); if (read == 0) break; readSoFar += read; } // we do it in this fashion to have the stream reader detect the BOM / unicode / other stuff // so we can read the values properly var fileText = new StreamReader(new MemoryStream(buffer,0, readSoFar)).ReadToEnd(); try { var disk = parser.Parse(fileText); addToBatch(disk); if (i++ % BatchSize == 0) Console.Write("\r{0} {1:#,#} {2} ", entry.Name, i, sp.Elapsed); } catch (Exception e) { Console.WriteLine(); Console.WriteLine(entry.Name); Console.WriteLine(e); return sp; } } } return sp; }
public override void Extract(Stream CompressedData, string OutputPath) { TarInputStream tarFile = new TarInputStream(inputDecompressor.Decompress(CompressedData)); if (!OutputPath.EndsWith(""+Path.DirectorySeparatorChar)) OutputPath = OutputPath + Path.DirectorySeparatorChar; while (true) { TarEntry entry = tarFile.GetNextEntry(); if (entry == null) break; string outputFile = OutputPath + entry.Name; if (entry.IsDirectory) continue; EnsureDirectoryExists(Path.GetDirectoryName(outputFile)); CopyStream(tarFile, File.OpenWrite(outputFile)); } }
/// <summary> /// Prepares to extract a TAR archive contained in a stream. /// </summary> /// <param name="stream">The stream containing the archive data to be extracted. Will be disposed when the extractor is disposed.</param> /// <param name="target">The path to the directory to extract into.</param> /// <exception cref="IOException">The archive is damaged.</exception> internal TarExtractor([NotNull] Stream stream, [NotNull] string target) : base(target) { #region Sanity checks if (stream == null) throw new ArgumentNullException(nameof(stream)); #endregion UnitsTotal = stream.Length; try { _tarStream = new TarInputStream(stream); } catch (SharpZipBaseException ex) { // Wrap exception since only certain exception types are allowed throw new IOException(Resources.ArchiveInvalid, ex); } }
static FFTPack() { using( MemoryStream memStream = new MemoryStream( PatcherLib.Properties.Resources.Resources_tar, false ) ) using( GZipInputStream gzStream = new GZipInputStream( memStream ) ) using ( TarInputStream tarStream = new TarInputStream( gzStream ) ) { TarEntry entry = tarStream.GetNextEntry(); while ( entry != null ) { if ( entry.Name == "FFTPackFiles.xml" ) { XmlDocument doc = new XmlDocument(); doc.Load( tarStream ); resourcesDoc = doc; } entry = tarStream.GetNextEntry(); } } }
public void Checksum() { MemoryStream ms = new MemoryStream(); TarOutputStream tarOut = new TarOutputStream(ms); DateTime modTime = DateTime.Now; TarEntry entry = TarEntry.CreateTarEntry("TestEntry"); entry.TarHeader.Mode = 12345; tarOut.PutNextEntry(entry); tarOut.Close(); MemoryStream ms2 = new MemoryStream(); ms2.Write(ms.GetBuffer(), 0, ms.GetBuffer().Length); ms2.Seek(0, SeekOrigin.Begin); TarInputStream tarIn = new TarInputStream(ms2); TarEntry nextEntry = tarIn.GetNextEntry(); Assert.IsTrue(nextEntry.TarHeader.IsChecksumValid, "Checksum should be valid"); MemoryStream ms3 = new MemoryStream(); ms3.Write(ms.GetBuffer(), 0, ms.GetBuffer().Length); ms3.Seek(0, SeekOrigin.Begin); ms3.Write(new byte[1] { 34 }, 0, 1); ms3.Seek(0, SeekOrigin.Begin); tarIn = new TarInputStream(ms3); bool trapped = false; try { nextEntry = tarIn.GetNextEntry(); } catch (TarException) { trapped = true; } Assert.IsTrue(trapped, "Checksum should be invalid"); }
private static void ParseDisks(BulkInsertOperation insert) { int i = 0; var parser = new Parser(); var buffer = new byte[1024*1024];// more than big enough for all files using (var bz2 = new BZip2InputStream(File.Open(@"D:\Scratch\freedb-complete-20150101.tar.bz2", FileMode.Open))) using (var tar = new TarInputStream(bz2)) { TarEntry entry; while((entry=tar.GetNextEntry()) != null) { if(entry.Size == 0 || entry.Name == "README" || entry.Name == "COPYING") continue; var readSoFar = 0; while(true) { var read = tar.Read(buffer, readSoFar, ((int) entry.Size) - readSoFar); if (read == 0) break; readSoFar += read; } // we do it in this fashion to have the stream reader detect the BOM / unicode / other stuff // so we can read the values properly var fileText = new StreamReader(new MemoryStream(buffer,0, readSoFar)).ReadToEnd(); try { var disk = parser.Parse(fileText); insert.Store(disk); } catch (Exception e) { Console.WriteLine(); Console.WriteLine(entry.Name); Console.WriteLine(e); } } } }
static Resources() { using ( MemoryStream memStream = new MemoryStream( FFTPatcher.TextEditor.Properties.Resources.Resources_tar, false ) ) using ( GZipInputStream gzStream = new GZipInputStream( memStream ) ) using ( TarInputStream tarStream = new TarInputStream( gzStream ) ) { TarEntry entry; entry = tarStream.GetNextEntry(); while ( entry != null ) { if ( entry.Size != 0 && !string.IsNullOrEmpty( entry.Name ) ) { XmlDocument doc = new XmlDocument(); doc.Load( tarStream ); resourceMapping[entry.Name] = doc; } entry = tarStream.GetNextEntry(); } } }
/// <summary> /// The InputStream based constructors create a TarArchive for the /// purposes of extracting or listing a tar archive. Thus, use /// these constructors when you wish to extract files from or list /// the contents of an existing tar archive. /// </summary> /// <param name="inputStream">The stream to retrieve archive data from.</param> /// <returns>Returns a new <see cref="TarArchive"/> suitable for reading from.</returns> public static TarArchive CreateInputTarArchive(Stream inputStream) { if (inputStream == null) { throw new ArgumentNullException("inputStream"); } TarInputStream tarStream = inputStream as TarInputStream; TarArchive result; if (tarStream != null) { result = new TarArchive(tarStream); } else { result = CreateInputTarArchive(inputStream, TarBuffer.DefaultBlockFactor); } return(result); }
public override bool Execute() { if (!NeedsUpdate()) return true; try { var ms = new MemoryStream(); var downloadStream = new System.Net.WebClient().OpenRead(Url); downloadStream.CopyTo(ms); ms.Seek(0, SeekOrigin.Begin); var hash = new SHA256Managed().ComputeHash(ms); if (BitConverter.ToString(hash).Replace("-", "").ToLower() != this.Hash) { Log.LogError("Got wrong hash for {0}", Url); return false; } try { Directory.Delete(Root, true); } catch (DirectoryNotFoundException) { // Obviously not an issue } ms.Seek(0, SeekOrigin.Begin); var bzStream = new ICSharpCode.SharpZipLib.BZip2.BZip2InputStream(ms); var tarStream = new ICSharpCode.SharpZipLib.Tar.TarInputStream(bzStream); while (true) { TarEntry entry = tarStream.GetNextEntry(); if (entry == null) break; ExtractEntry(Root, entry, tarStream); } File.WriteAllText(Path.Combine(Root, "aegisub.hash"), Hash); return true; } catch (Exception e) { Log.LogErrorFromException(e); return false; } }
public void Unir(FileInfo fichero, DirectoryInfo dirDest) { DalleStream dstream = new DalleStream (fichero); byte[] buffer = new byte[Consts.BUFFER_LENGTH]; OnProgress (0, dstream.Length); Stream gzipStream = new GZipStream (dstream, CompressionMode.Decompress); TarInputStream tarStream = new TarInputStream (gzipStream); TarEntry tarEntry = null; OnProgress (0, 1); while ((tarEntry = tarStream.GetNextEntry ()) != null) { // Tamaño de la cabecera de la entrada. // Nota: TarInputStream ignora sileciosamente algunas entradas, // por lo que el progreso no será totalmente preciso. if (tarEntry.IsDirectory) { continue; } Stream entrada = new SizeLimiterStream (tarStream, tarEntry.Size); Stream salida = UtilidadesFicheros.CreateWriter (dirDest.FullName + Path.DirectorySeparatorChar + tarEntry.Name); int leidos = 0; while ((leidos = entrada.Read (buffer, 0, buffer.Length)) > 0) { salida.Write (buffer, 0, leidos); OnProgress (dstream.Position, dstream.Length+1); // +1 para evitar llegar al 100% antes de tiempo } salida.Close (); } tarStream.Close (); OnProgress (1, 1); }
private void ReadFile() { var buffer = new byte[1024 * 1024];// more than big enough for all files using (var bz2 = new BZip2InputStream(File.Open(_path, FileMode.Open))) using (var tar = new TarInputStream(bz2)) { TarEntry entry; while ((entry = tar.GetNextEntry()) != null) { if (entry.Size == 0 || entry.Name == "README" || entry.Name == "COPYING") continue; var readSoFar = 0; while (true) { var bytes = tar.Read(buffer, readSoFar, ((int)entry.Size) - readSoFar); if (bytes == 0) break; readSoFar += bytes; } // we do it in this fashion to have the stream reader detect the BOM / unicode / other stuff // so we can reads the values properly var fileText = new StreamReader(new MemoryStream(buffer, 0, readSoFar)).ReadToEnd(); _entries.Add(fileText); Interlocked.Increment(ref reads); } } _entries.Add(null); }
/// <summary> /// Updates the supplied registry from the supplied zip file. /// This will *clear* the registry of available modules first. /// This does not *save* the registry. For that, you probably want Repo.Update /// </summary> internal static void UpdateRegistryFromTarGz(string path, Registry registry) { log.DebugFormat("Starting registry update from tar.gz file: \"{0}\".", path); // Open the gzip'ed file. using (Stream inputStream = File.OpenRead(path)) { // Create a gzip stream. using (GZipInputStream gzipStream = new GZipInputStream(inputStream)) { // Create a handle for the tar stream. using (TarInputStream tarStream = new TarInputStream(gzipStream)) { // Walk the archive, looking for .ckan files. const string filter = @"\.ckan$"; while (true) { TarEntry entry = tarStream.GetNextEntry(); // Check for EOF. if (entry == null) { break; } string filename = entry.Name; // Skip things we don't want. if (!Regex.IsMatch(filename, filter)) { log.DebugFormat("Skipping archive entry {0}", filename); continue; } log.DebugFormat("Reading CKAN data from {0}", filename); // Read each file into a buffer. int buffer_size; try { buffer_size = Convert.ToInt32(entry.Size); } catch (OverflowException) { log.ErrorFormat("Error processing {0}: Metadata size too large.", entry.Name); continue; } byte[] buffer = new byte[buffer_size]; tarStream.Read(buffer, 0, buffer_size); // Convert the buffer data to a string. string metadata_json = Encoding.ASCII.GetString(buffer); ProcessRegistryMetadataFromJSON(metadata_json, registry, filename); } } } } }
private IEnumerable<GutCatDoc> getRdfFiles() { var req = (HttpWebRequest)WebRequest.Create(DownloadUrl); using (var resp = (HttpWebResponse)req.GetResponse()) { using (var zip = new ZipInputStream(resp.GetResponseStream())) { zip.GetNextEntry(); using (var tar = new TarInputStream(zip)) { TarEntry tarentry; while ((tarentry = tar.GetNextEntry()) != null) { if (tarentry.IsDirectory) continue; yield return new GutCatDoc(tar); } } } } }
void UnpackTar(Stream strm) { TarInputStream tar = new TarInputStream(strm); byte[] tempBuf = new byte[65536]; progressBar2.Style = ProgressBarStyle.Marquee; int done = 0; for (; ; ) { TarEntry entry = tar.GetNextEntry(); if (entry == null) break; string strName = entry.Name; string firstComponent = strName.Substring(0, strName.IndexOf('/')); if (firstComponent.ToUpper() == m_NamePrefixToDrop.ToUpper()) strName = strName.Substring(m_NamePrefixToDrop.Length + 1); if (strName == "") continue; string fn = m_Dir + "\\" + strName; if ((_Filter == null) || (_Filter(strName))) if (entry.IsDirectory) { if (!Directory.Exists(fn)) Directory.CreateDirectory(fn); } else using (FileStream ostrm = new FileStream(fn, FileMode.Create)) tar.CopyEntryContents(ostrm); if ((done += (int)entry.Size) > progressBar2.Maximum) done = progressBar2.Maximum; progressBar2.Value = done; Application.DoEvents(); } }
private Stopwatch ParseDisks(Action<Disk> addToBatch) { int i = 0; var parser = new Parser(); var buffer = new byte[1024 * 1024];// more than big enough for all files var sp = Stopwatch.StartNew(); using (var bz2 = new BZip2InputStream(File.Open(dataLocation, FileMode.Open))) using (var tar = new TarInputStream(bz2)) { TarEntry entry; while ((entry = tar.GetNextEntry()) != null) { if (entry.Size == 0 || entry.Name == "README" || entry.Name == "COPYING") continue; var readSoFar = 0; while (true) { var read = tar.Read(buffer, readSoFar, ((int)entry.Size) - readSoFar); if (read == 0) break; readSoFar += read; } // we do it in this fashion to have the stream reader detect the BOM / unicode / other stuff // so we can read the values properly var fileText = new StreamReader(new MemoryStream(buffer, 0, readSoFar)).ReadToEnd(); try { var disk = parser.Parse(fileText); addToBatch(disk); if (i++ % BatchSize == 0) { process.Refresh(); MemoryUsage.Add(process.WorkingSet64); logger.Info("\r{0} {1:#,#} {2} ", entry.Name, i, sp.Elapsed); } } catch (Exception e) { logger.Error(""); logger.Error(entry.Name); logger.Error(e); return sp; } } } return sp; }
/// <summary> /// tar包解压 /// </summary> /// <param name="strFilePath">tar包路径</param> /// <param name="strUnpackDir">解压到的目录</param> /// <returns></returns> public bool UnpackTarFiles(string strFilePath, string strUnpackDir) { try { if (!File.Exists(strFilePath)) { return(false); } strUnpackDir = strUnpackDir.Replace("/", "\\"); if (!strUnpackDir.EndsWith("\\")) { strUnpackDir += "\\"; } if (!Directory.Exists(strUnpackDir)) { Directory.CreateDirectory(strUnpackDir); } FileStream fr = new FileStream(strFilePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite); ICSharpCode.SharpZipLib.Tar.TarInputStream s = new ICSharpCode.SharpZipLib.Tar.TarInputStream(fr); ICSharpCode.SharpZipLib.Tar.TarEntry theEntry; while ((theEntry = s.GetNextEntry()) != null) { string directoryName = Path.GetDirectoryName(theEntry.Name); string fileName = Path.GetFileName(theEntry.Name); if (directoryName != String.Empty) { Directory.CreateDirectory(strUnpackDir + directoryName); } if (fileName != String.Empty) { FileStream streamWriter = File.Create(strUnpackDir + theEntry.Name); int size = 2048; byte[] data = new byte[2048]; while (true) { size = s.Read(data, 0, data.Length); if (size > 0) { streamWriter.Write(data, 0, size); } else { break; } } streamWriter.Close(); } } s.Close(); fr.Close(); return(true); } catch (Exception) { return(false); } }
/// <summary> /// Enumerate through the files of a TAR and get a list of KVP names-byte arrays /// </summary> /// <param name="stream">The input tar stream</param> /// <param name="isTarGz">True if the input stream is a .tar.gz or .tgz</param> /// <returns>An enumerable containing each tar entry and it's contents</returns> public static IEnumerable<KeyValuePair<string, byte[]>> UnTar(Stream stream, bool isTarGz) { using (var tar = new TarInputStream(isTarGz ? (Stream)new GZipInputStream(stream) : stream)) { TarEntry entry; while ((entry = tar.GetNextEntry()) != null) { if (entry.IsDirectory) continue; using (var output = new MemoryStream()) { tar.CopyEntryContents(output); yield return new KeyValuePair<string, byte[]>(entry.Name, output.ToArray()); } } } }
// TODO: Create a class Archive.cs and do all the archiving stuff there! This is just copy and paste crap public static ArrayList UncompressTarFile(string Filename, string To, Gtk.ProgressBar bar) { ArrayList entries=new ArrayList(); try{ TarInputStream tarIn = new TarInputStream(File.OpenRead(Filename)); TarEntry entry; while ((entry = tarIn.GetNextEntry()) != null) { string savepath = Path.GetDirectoryName(To+entry.Name); if (!Directory.Exists(savepath)){ Directory.CreateDirectory(savepath); //Console.WriteLine(Path.GetDirectoryName(entry.Name)); } entries.Add(Path.GetDirectoryName(entry.Name)); if (!entry.IsDirectory) { FileStream streamWriter = File.Create(To + entry.Name); long size = entry.Size; byte[] data = new byte[size]; while (true) { size = tarIn.Read(data, 0, data.Length); if (size > 0) streamWriter.Write(data, 0, (int) size); else break; } streamWriter.Close(); } } Console.WriteLine("Deflating the tar file done!"); return entries; } catch(Exception e) { Console.WriteLine("An exception occured while deflating the tar file: "+e.Message); return entries; } }
public void ValuesPreserved() { MemoryStream ms = new MemoryStream(); TarEntry entry; DateTime modTime = DateTime.Now; using (TarOutputStream tarOut = new TarOutputStream(ms)) { entry = TarEntry.CreateTarEntry("TestEntry"); entry.GroupId = 12; entry.UserId = 14; entry.ModTime = modTime; entry.UserName = "******"; entry.GroupName = "GroupName"; entry.TarHeader.Mode = 12345; tarOut.PutNextEntry(entry); } MemoryStream ms2 = new MemoryStream(); ms2.Write(ms.GetBuffer(), 0, ms.GetBuffer().Length); ms2.Seek(0, SeekOrigin.Begin); using (TarInputStream tarIn = new TarInputStream(ms2)) { TarEntry nextEntry = tarIn.GetNextEntry(); Assert.AreEqual(entry.TarHeader.Checksum, nextEntry.TarHeader.Checksum, "Checksum"); Assert.IsTrue(nextEntry.Equals(entry), "Entries should be equal"); Assert.IsTrue(nextEntry.TarHeader.Equals(entry.TarHeader), "Headers should match"); // Tar only stores seconds DateTime truncatedTime = new DateTime(modTime.Year, modTime.Month, modTime.Day, modTime.Hour, modTime.Minute, modTime.Second); Assert.AreEqual(truncatedTime, nextEntry.ModTime, "Modtimes should match"); int entryCount = 0; while ( nextEntry != null ) { ++entryCount; nextEntry = tarIn.GetNextEntry(); } Assert.AreEqual(1, entryCount, "Expected 1 entry"); } }
public SharpZipTarArchiveIterator() { tarStream = null; disposed = true; }
private static void GetTarMetadata(Stream s, List<MetadataItem> metadata) { using (TarInputStream s2 = new TarInputStream(s)) { TarEntry e; while ((e = s2.GetNextEntry()) != null) { // Note: directories can be identified by a ending slash metadata.Add(new MetadataItem(MetadataType.FILENAME, e.Name)); } } }
/// <summary> /// Initialise a TarArchive for input. /// </summary> /// <param name="stream">The <see cref="TarInputStream"/> to use for input.</param> protected TarArchive(TarInputStream stream) { tarIn = stream ?? throw new ArgumentNullException(nameof(stream)); }
public SharpZipTarArchiveIterator(Stream tarFile) { tarStream = new TarInputStream(tarFile); disposed = false; }
List<TestCase> AddTestCase(Contest contest, Problem problem, HttpPostedFileBase file) { if (file == null) { ModelState.AddModelError("File", "请选择文件"); return null; } Dictionary<int, byte[]> inputFiles = new Dictionary<int, byte[]>(); Dictionary<int, byte[]> outputFiles = new Dictionary<int, byte[]>(); if (new[] { "application/zip", "application/x-zip-compressed", "application/x-zip" }.Contains(file.ContentType) || file.ContentType == "application/octet-stream" && file.FileName.EndsWith(".zip", StringComparison.OrdinalIgnoreCase)) { using (ZipInputStream stream = new ZipInputStream(file.InputStream)) { ZipEntry entry; while ((entry = stream.GetNextEntry()) != null) { byte[] bytes; using (MemoryStream mem = new MemoryStream()) { stream.CopyTo(mem); bytes = mem.ToArray(); } if (!DealEntry(entry.Name, bytes, inputFiles, outputFiles)) { return null; } } } } else if (file.FileName.EndsWith(".tgz") || file.FileName.EndsWith(".tar.gz")) { using (GZipStream stream = new GZipStream(file.InputStream, CompressionMode.Decompress)) { using (TarInputStream tar = new TarInputStream(stream)) { TarEntry entry; while ((entry = tar.GetNextEntry()) != null) { byte[] bytes; using (MemoryStream mem = new MemoryStream()) { tar.CopyTo(mem); bytes = mem.ToArray(); } if (!DealEntry(entry.Name, bytes, inputFiles, outputFiles)) { return null; } } } } } else if (file.FileName.EndsWith(".tar.bz2")) { using (BZip2InputStream stream = new BZip2InputStream(file.InputStream)) { using (TarInputStream tar = new TarInputStream(stream)) { TarEntry entry; while ((entry = tar.GetNextEntry()) != null) { byte[] bytes; using (MemoryStream mem = new MemoryStream()) { tar.CopyTo(mem); bytes = mem.ToArray(); } if (!DealEntry(entry.Name, bytes, inputFiles, outputFiles)) { return null; } } } } } else { ModelState.AddModelError("File", "不支持的压缩文件类型"); return null; } if (!inputFiles.Keys.OrderBy(x => x).SequenceEqual(outputFiles.Keys.OrderBy(x => x))) { ModelState.AddModelError("File", "输入与输出文件没有一一对应"); return null; } var testCases = inputFiles.Keys.Select(id => new TestCase { Input = inputFiles[id], Data = outputFiles[id], MemoryLimit = DEFAULT_TEST_CASE_MEMORY_LIMIT, TimeLimit = DEFAULT_TEST_CASE_TIME_LIMIT, Available = contest.Type == Contest.ContestType.CF ? false : true }).ToList(); foreach (var t in testCases) { t.ID = problem.AddTestCase(t); } return testCases; }
void TryLongName(string name) { MemoryStream ms = new MemoryStream(); using ( TarOutputStream tarOut = new TarOutputStream(ms) ) { DateTime modTime = DateTime.Now; TarEntry entry = TarEntry.CreateTarEntry(name); tarOut.PutNextEntry(entry); } MemoryStream ms2 = new MemoryStream(); ms2.Write(ms.GetBuffer(), 0, ms.GetBuffer().Length); ms2.Seek(0, SeekOrigin.Begin); using (TarInputStream tarIn = new TarInputStream(ms2)) { TarEntry nextEntry = tarIn.GetNextEntry(); Assert.AreEqual(nextEntry.Name, name, "Name match failure"); } }
/// <summary> /// Creates a new TarFileSystem /// </summary> /// <param name="stream">The tar input stream</param> public TarFileSystem(Stream stream) { this.tis = new TarInputStream(stream); PreloadTOC(); }
public void InputStreamOwnership() { TrackedMemoryStream memStream = new TrackedMemoryStream(); TarInputStream s = new TarInputStream(memStream); Assert.IsFalse(memStream.IsClosed, "Shouldnt be closed initially"); Assert.IsFalse(memStream.IsDisposed, "Shouldnt be disposed initially"); s.Close(); Assert.IsTrue(memStream.IsClosed, "Should be closed after parent owner close"); Assert.IsTrue(memStream.IsDisposed, "Should be disposed after parent owner close"); memStream = new TrackedMemoryStream(); s = new TarInputStream(memStream); Assert.IsFalse(memStream.IsClosed, "Shouldnt be closed initially"); Assert.IsFalse(memStream.IsDisposed, "Shouldnt be disposed initially"); s.IsStreamOwner = false; s.Close(); Assert.IsFalse(memStream.IsClosed, "Should not be closed after parent owner close"); Assert.IsFalse(memStream.IsDisposed, "Should not be disposed after parent owner close"); }
/// <summary> /// Extracts the files from the archive. /// </summary> protected override void ExecuteTask() { Stream fs = null; Stream instream = null; try { // ensure archive exists if (!SrcFile.Exists) throw new BuildException(string.Format(CultureInfo.InvariantCulture, "Tar file '{0}' does not exist.", SrcFile.FullName), Location); fs = SrcFile.OpenRead(); // wrap inputstream with corresponding compression method switch (CompressionMethod) { case TarCompressionMethod.GZip: instream = new GZipInputStream(fs); break; case TarCompressionMethod.BZip2: instream = new BZip2InputStream(fs); break; default: instream = fs; break; } using (TarInputStream s = new TarInputStream(instream)) { Log(Level.Info, "Expanding '{0}' to '{1}'.", SrcFile.FullName, DestinationDirectory.FullName); TarEntry entry; // extract the file or directory entry while ((entry = s.GetNextEntry()) != null) { if (entry.IsDirectory) { ExtractDirectory(s, DestinationDirectory.FullName, entry.Name, entry.ModTime); } else { ExtractFile(s, DestinationDirectory.FullName, entry.Name, entry.ModTime, entry.Size); } } } } catch (IOException ex) { throw new BuildException(string.Format(CultureInfo.InvariantCulture, "Failed to expand '{0}' to '{1}'.", SrcFile.FullName, DestinationDirectory.FullName), Location, ex); } catch (TarException ex) { throw new BuildException(string.Format(CultureInfo.InvariantCulture, "Invalid tar file '{0}'.", SrcFile.FullName), Location, ex); } catch (BZip2Exception ex) { throw new BuildException(string.Format(CultureInfo.InvariantCulture, "Invalid bzip2'd tar file '{0}'.", SrcFile.FullName), Location, ex); } catch (GZipException ex) { throw new BuildException(string.Format(CultureInfo.InvariantCulture, "Invalid gzipped tar file '{0}'.", SrcFile.FullName), Location, ex); } finally { // close the filestream if (fs != null) fs.Close (); } }
/// <summary> /// Enumerate through the files of a TAR and get a list of KVP names-byte arrays. /// </summary> /// <param name="source"></param> /// <returns></returns> public static IEnumerable<KeyValuePair<string, byte[]>> UnTar(string source) { //This is a tar.gz file. var gzip = (source.Substring(Math.Max(0, source.Length - 6)) == "tar.gz"); using (var file = File.OpenRead(source)) { var tarIn = new TarInputStream(file); if (gzip) { var gzipStream = new GZipInputStream(file); tarIn = new TarInputStream(gzipStream); } TarEntry tarEntry; while ((tarEntry = tarIn.GetNextEntry()) != null) { if (tarEntry.IsDirectory) continue; using (var stream = new MemoryStream()) { tarIn.CopyEntryContents(stream); yield return new KeyValuePair<string, byte[]>(tarEntry.Name, stream.ToArray()); } } tarIn.Close(); } }
public override void SetBaseStream(Stream stream) { tarStream = new TarInputStream(stream); disposed = false; }