private static void CopyWithAsciiTranslate(ICSharpCode.SharpZipLib.Tar.TarInputStream tarIn, System.IO.Stream outStream) { byte[] buffer = new byte[4096]; bool isAscii = true; bool cr = false; int numRead = tarIn.Read(buffer, 0, buffer.Length); int maxCheck = System.Math.Min(200, numRead); for (int i = 0; i < maxCheck; i++) { byte b = buffer[i]; if (b < 8 || (b > 13 && b < 32) || b == 255) { isAscii = false; break; } } while (numRead > 0) { if (isAscii) { // Convert LF without CR to CRLF. Handle CRLF split over buffers. for (int i = 0; i < numRead; i++) { byte b = buffer[i]; // assuming plain Ascii and not UTF-16 if (b == 10 && !cr) // LF without CR { outStream.WriteByte(13); } cr = (b == 13); outStream.WriteByte(b); } } else { outStream.Write(buffer, 0, numRead); } numRead = tarIn.Read(buffer, 0, buffer.Length); } }
/// <summary> /// /// </summary> /// <param name="data"></param> /// <returns></returns> public static byte[] DecompressBytes(byte[] data) { var o = new MemoryStream(); var s = new ICSharpCode.SharpZipLib.Tar.TarInputStream(new MemoryStream(data)); try { var size = 0; var buf = new byte[1024]; while ((size = s.Read(buf, 0, buf.Length)) > 0) { o.Write(buf, 0, size); } } finally { o.Close(); } return(o.ToArray()); }
/// <summary> /// tar包解压 /// </summary> /// <param name="strFilePath">tar包路径</param> /// <param name="strUnpackDir">解压到的目录</param> /// <returns></returns> public static 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); } using (FileStream fr = new FileStream(strFilePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) { using (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(); } } } } return(true); } catch { return(false); } }