/// <summary> /// Close the archive. This simply calls the underlying /// tar stream's close() method. /// </summary> public void CloseArchive() { if (tarIn != null) { tarIn.Close(); } else if (tarOut != null) { tarOut.Flush(); tarOut.Close(); } }
protected virtual void Dispose(bool disposing) { if (isDisposed) { return; } isDisposed = true; if (disposing) { if (tarOut != null) { tarOut.Flush(); tarOut.Close(); } if (tarIn != null) { tarIn.Close(); } } }
/// <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); } }
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"); }
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 extracts(Object arg) { List<string> compressedfiles = (List<string>)arg; foreach (string source in compressedfiles) { string desitination = Path.GetDirectoryName(source); if (source.EndsWith(".tgz") == true || source.EndsWith(".tar.gz") == true) { using (GZipInputStream tgz = new GZipInputStream(new FileStream(source, FileMode.Open,FileAccess.Read))) { using (TarInputStream tar = new TarInputStream(tgz)) { extract_tar(tar, desitination); tar.Close(); } tgz.Close(); } } else if (source.EndsWith(".tar")) { /** for not hasty man using (ICSharpCode.SharpZipLib.Tar.TarArchive ta = TarArchive.CreateInputTarArchive(new TarInputStream(new FileStream(source, FileMode.Open, FileAccess.Read)))) { ta.ProgressMessageEvent += new ProgressMessageHandler(tar_ProgressMessageEvent); ta.ExtractContents(desitination); } */ using (TarInputStream tar = new TarInputStream(new FileStream(source, FileMode.Open, FileAccess.Read))) { extract_tar(tar, desitination); tar.Close(); } } #if _USE_ICONIC_ else if (source.EndsWith(".zip")) { /** for not hasty man using (Ionic.Zip.ZipFile zip = Ionic.Zip.ZipFile.Read(source)) { foreach (Ionic.Zip.ZipEntry entry in zip) { entry.Extract(desitination, ExtractExistingFileAction.OverwriteSilently); } } */ using (Ionic.Zip.ZipFile zip = Ionic.Zip.ZipFile.Read(source)) { zip.ExtractProgress += new EventHandler<Ionic.Zip.ExtractProgressEventArgs>(extract_zip); foreach (Ionic.Zip.ZipEntry entry in zip) { Invoke(new delegateUpdateLabel((string s) => { this.label1.Text = s; }), new Object[] { entry.FileName + " extracting ..." }); entry.Extract(desitination, ExtractExistingFileAction.OverwriteSilently); } } } #else else if (source.EndsWith(".zip")) { //and so on... ** but! ** see. http://community.sharpdevelop.net/forums/t/11466.aspx } #endif } Invoke(new threadTerminated(extract_terminated), compressedfiles.Count); }
protected override void _Unir(string fichero, string dirDest) { if (!File.Exists (fichero)) { return; } FileInfo fi = new FileInfo (fichero); long datosTotales = fi.Length; long uncompressedSize = 0; FileStream input = File.OpenRead (fichero); Stream input2 = input; if (fichero.ToLower ().EndsWith (".bz2") || fichero.ToLower ().EndsWith (".tbz2") || fichero.ToLower ().EndsWith (".tbz")) { // No hay forma de saber el tamaño descomprimido de un bz2 de forma inmediata input2 = new BZip2InputStream (input); } else if (fichero.ToLower ().EndsWith (".gz") || fichero.ToLower ().EndsWith (".tgz")) { uncompressedSize = Dalle.Formatos.GZip.GZip.GetUncompressedSize (input); input2 = new GZipStream (input, CompressionMode.Decompress); } else if (fichero.ToLower ().EndsWith (".tar.lzma") || fichero.ToLower ().EndsWith ("tlz")) { input2 = new LZMAInputStream (input); uncompressedSize = ((LZMAInputStream)input2).UncompressedSize; } TarInputStream tarInput = new TarInputStream (input2); TarEntry tarEntry = null; byte[] buffer = new byte[Consts.BUFFER_LENGTH]; OnProgress (0, 1); long transferidos = 0; while ((tarEntry = tarInput.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. transferidos += 512; if (tarEntry.IsDirectory) { continue; } Stream entrada = new SizeLimiterStream (tarInput, tarEntry.Size); Stream salida = UtilidadesFicheros.CreateWriter (dirDest + Path.DirectorySeparatorChar + tarEntry.Name); int leidos = 0; while ((leidos = entrada.Read (buffer, 0, buffer.Length)) > 0) { salida.Write (buffer, 0, leidos); transferidos += leidos; if (uncompressedSize > 0) { OnProgress (transferidos, uncompressedSize); } else { OnProgress (input.Position, datosTotales); } } salida.Close (); transferidos += 512 - (tarEntry.Size % 512); } tarInput.Close (); OnProgress (1, 1); }
private void processTask(RestoreTask task) { Logger.Debug("StorageThread:processTask:RestoreTask"); Stream inStream = File.OpenRead(task.ArchivePath); Stream gzipStream = new GZipInputStream(inStream); TarInputStream tarStream = new TarInputStream(gzipStream); TarEntry entry; List<string> list = task.RelativeFilenames(); RecoverResult recover = new RecoverResult(task.OutputDir, true); while ((entry = tarStream.GetNextEntry()) != null) { if (entry.IsDirectory) continue; if (list.IndexOf(entry.Name) != -1) { string name = entry.Name.Replace('/', Path.DirectorySeparatorChar); name = Path.Combine(task.OutputDir, name); Directory.CreateDirectory(Path.GetDirectoryName(name)); FileStream outStream = new FileStream(name, FileMode.CreateNew); tarStream.CopyEntryContents(outStream); outStream.Close(); DateTime myDt = DateTime.SpecifyKind(entry.ModTime, DateTimeKind.Utc); File.SetLastWriteTime(name, myDt); } } tarStream.Close(); lock (_lock) { recoverResults.Enqueue(recover); } }