public static void UnzipFile(string filePathToUnzip, string unzipedFilePath) { CheckPath(filePathToUnzip, unzipedFilePath); using (ZipInputStream s = new ZipInputStream(File.OpenRead(filePathToUnzip))) { ZipEntry entry = s.GetNextEntry(); UtilsFunc.Assert(entry != null, "entry is null"); using (FileStream outputStream = File.Create(unzipedFilePath)) { int size = 0; byte[] data = new byte[2048]; while (true) { size = s.Read(data, 0, data.Length); if (size > 0) { outputStream.Write(data, 0, size); } else { break; } } } // No Dir supported in this function. UtilsFunc.Assert(null == s.GetNextEntry()); } }
private static void CheckPath(string inputFilePath, string outputFilePath) { UtilsFunc.Assert(!String.IsNullOrEmpty(inputFilePath)); UtilsFunc.Assert(!String.IsNullOrEmpty(outputFilePath)); UtilsFunc.Assert(File.Exists(inputFilePath)); UtilsFunc.Assert(!File.Exists(outputFilePath)); UtilsFunc.Assert(Directory.Exists(Path.GetDirectoryName(outputFilePath))); }
public static byte[] ZipBytes(byte[] input, int compressLevel) { UtilsFunc.Assert(input != null); Deflater compressor = new Deflater(); compressor.SetLevel(compressLevel); compressor.SetInput(input); compressor.Finish(); byte[] ret = null; using (MemoryStream bos = new MemoryStream(input.Length)) { byte[] buf = new byte[1024]; while (!compressor.IsFinished) { int count = compressor.Deflate(buf); bos.Write(buf, 0, count); } ret = bos.ToArray(); } return(ret); }