public static void unZip(byte[] dataZip, string path) { try { using (Stream zipFile = new MemoryStream(dataZip)) { using (ICSharpCode.SharpZipLib.Zip.ZipInputStream ZipStream = new ICSharpCode.SharpZipLib.Zip.ZipInputStream(zipFile)) { ICSharpCode.SharpZipLib.Zip.ZipEntry theEntry; theEntry = ZipStream.GetNextEntry(); if (theEntry.IsFile) { if (theEntry.Name != "") { FileStream outputStream = new FileStream(path, FileMode.OpenOrCreate); StreamUtils.Copy(ZipStream, outputStream, new byte[4096]); ZipStream.Close(); outputStream.Close(); } } } } } catch (Exception ex) { //Console.WriteLine(ex.Message); throw ex; } }
public string[] retornaListaDeArquivos(string strNomeArquivoZip) { string[] strArrayRetorno = new string[0]; try { ICSharpCode.SharpZipLib.Zip.ZipInputStream clsZipInStream = new ICSharpCode.SharpZipLib.Zip.ZipInputStream(System.IO.File.OpenRead(strNomeArquivoZip)); int nCount = 0; System.Collections.ArrayList arlArquivos = new System.Collections.ArrayList(); ICSharpCode.SharpZipLib.Zip.ZipEntry theEntry; while ((theEntry = clsZipInStream.GetNextEntry()) != null) { arlArquivos.Add(theEntry.Name); nCount++; } clsZipInStream.Close(); strArrayRetorno = new string[nCount]; int nIndice = 0; foreach (string strFile in arlArquivos) { strArrayRetorno[nIndice] = strFile; nIndice++; } } catch (Exception err) { Object erro = err; m_cls_ter_tratadorErro.trataErro(ref erro); } return(strArrayRetorno); }
/* Unzips dirName + ".zip" --> dirName, removing dirName * first */ public virtual void Unzip(System.String zipName, System.String destDirName) { #if SHARP_ZIP_LIB // get zip input stream ICSharpCode.SharpZipLib.Zip.ZipInputStream zipFile; zipFile = new ICSharpCode.SharpZipLib.Zip.ZipInputStream(System.IO.File.OpenRead(zipName + ".zip")); // get dest directory name System.String dirName = FullDir(destDirName); System.IO.FileInfo fileDir = new System.IO.FileInfo(dirName); // clean up old directory (if there) and create new directory RmDir(fileDir.FullName); System.IO.Directory.CreateDirectory(fileDir.FullName); // copy file entries from zip stream to directory ICSharpCode.SharpZipLib.Zip.ZipEntry entry; while ((entry = zipFile.GetNextEntry()) != null) { System.IO.Stream streamout = new System.IO.BufferedStream(new System.IO.FileStream(new System.IO.FileInfo(System.IO.Path.Combine(fileDir.FullName, entry.Name)).FullName, System.IO.FileMode.Create)); byte[] buffer = new byte[8192]; int len; while ((len = zipFile.Read(buffer, 0, buffer.Length)) > 0) { streamout.Write(buffer, 0, len); } streamout.Close(); } zipFile.Close(); #else Assert.Fail("Needs integration with SharpZipLib"); #endif }
public static byte[] ReadEntry(string zipFileName, string entryname, string password = null) { try { SharpZipLib.Zip.ZipInputStream s = new SharpZipLib.Zip.ZipInputStream(File.OpenRead(zipFileName)); s.Password = password; SharpZipLib.Zip.ZipEntry theEntry; if (!FindEntry(s, entryname, out theEntry)) { s.Close(); return(null); } byte[] buffer = null; using (MemoryStream mm = new MemoryStream()) { int size = 2048; byte[] data = new byte[2048]; while (true) { size = s.Read(data, 0, data.Length); if (size > 0) { mm.Write(data, 0, size); } else { break; } } buffer = mm.ToArray(); mm.Close(); } s.Close(); return(buffer); } catch (Exception e) { LogUtil.LogError(e.Message); return(null); } }
public void UnZIP(string SourceFile, string DestPath, bool PreservePath, string[] ZipEntry) { FileStream fs = File.OpenRead(SourceFile); ICSharpCode.SharpZipLib.Zip.ZipInputStream s = new ICSharpCode.SharpZipLib.Zip.ZipInputStream(fs); ICSharpCode.SharpZipLib.Zip.ZipEntry theEntry; while ((theEntry = s.GetNextEntry()) != null) { if (theEntry.IsDirectory) { continue; } if (null != ZipEntry && ZipEntry.Length > 0 && Array.BinarySearch(ZipEntry, Path.GetFileName(theEntry.Name), System.Collections.CaseInsensitiveComparer.Default) < 0) { continue; } int size = 2048; byte[] data = new byte[2048]; string outputFileName = null; if (PreservePath) { outputFileName = Path.Combine(DestPath, theEntry.Name); } else { outputFileName = Path.Combine(DestPath, Path.GetFileName(theEntry.Name)); } if (!Directory.Exists(Path.GetDirectoryName(outputFileName))) { Directory.CreateDirectory(Path.GetDirectoryName(outputFileName)); } FileStream destFS = new FileStream(outputFileName, FileMode.Create); while (s.Available > 0) { int readLen = s.Read(data, 0, size); destFS.Write(data, 0, readLen); } destFS.Flush(); destFS.Close(); } s.Close(); }
/// <summary> /// 解压缩(将压缩的文件解压到指定的文件夹下面)—解压到指定文件夹下面 /// </summary> /// <param name="zipFilePath">源压缩的文件路径</param> /// <param name="savePath">解压后保存文件到指定的文件夹</param> public static void ZipUnFileInfo(string zipFilePath, string savePath) { try { using (var zipInputStream = new ICSharpCode.SharpZipLib.Zip.ZipInputStream(File.OpenRead(zipFilePath))) { ICSharpCode.SharpZipLib.Zip.ZipEntry zipEntry; while ((zipEntry = zipInputStream.GetNextEntry()) != null) { string directoryName = Path.GetDirectoryName(zipEntry.Name); string fileName = Path.GetFileName(zipEntry.Name); string serverFolder = savePath; //创建一个文件目录信息 Directory.CreateDirectory(serverFolder + "/" + directoryName); //如果解压的文件不等于空,则执行以下步骤 if (fileName != string.Empty) { using (FileStream fileStream = File.Create((serverFolder + "/" + zipEntry.Name))) { int size = 2048; byte[] data = new byte[2048]; //初始化字节数为2兆,后面根据需要解压的内容扩展字节数 while (true) { size = zipInputStream.Read(data, 0, data.Length); if (size > 0) { fileStream.Write(data, 0, size); } else { break; } } fileStream.Close(); } } } zipInputStream.Close(); } } catch (Exception exception) { throw new Exception("出现错误了,不能解压缩,错误原因:" + exception.Message); } }
/// <summary> /// Разжатие /// </summary> /// <param name="ms"></param> /// <returns></returns> public static MemoryStream Decompress(MemoryStream ms) { ms.Seek(0, System.IO.SeekOrigin.Begin); ICSharpCode.SharpZipLib.Zip.ZipInputStream zs = new ICSharpCode.SharpZipLib.Zip.ZipInputStream(ms); ICSharpCode.SharpZipLib.Zip.ZipEntry theEntry; try { theEntry = zs.GetNextEntry(); } catch (Exception e) { return(ms); } MemoryStream resstream = new MemoryStream(); //**** byte[] data = new byte[4096]; int size; do { size = zs.Read(data, 0, data.Length); resstream.Write(data, 0, size); } while (size > 0); //***** //b = new byte[zs.Length]; //zs.Read(b,0,b.Length); //resstream.Write(b,0,b.Length); resstream.Seek(0, SeekOrigin.Begin); zs.Close(); return(resstream); }
public void descompacta(string strNomeArquivoZip) { try { ICSharpCode.SharpZipLib.Zip.ZipInputStream clsZipInStream = new ICSharpCode.SharpZipLib.Zip.ZipInputStream(System.IO.File.OpenRead(strNomeArquivoZip)); ICSharpCode.SharpZipLib.Zip.ZipEntry theEntry; while ((theEntry = clsZipInStream.GetNextEntry()) != null) { if (theEntry.Size > 0) { byte[] conteudoArquivo = new byte[theEntry.Size]; int bytesReaded, posInFile = 0; long fileSize = theEntry.Size; do { bytesReaded = clsZipInStream.Read(conteudoArquivo, posInFile, (int)fileSize); posInFile += bytesReaded; fileSize -= bytesReaded; } while (fileSize > 0 || bytesReaded == 0); if (bytesReaded > 0) { System.IO.FileStream fsOutFile = new System.IO.FileStream(theEntry.Name, System.IO.FileMode.Create, System.IO.FileAccess.Write); System.IO.BinaryWriter bwOutFile = new System.IO.BinaryWriter(fsOutFile); bwOutFile.Write(conteudoArquivo); bwOutFile.Flush(); bwOutFile.Close(); fsOutFile.Close(); } conteudoArquivo = null; } } clsZipInStream.Close(); } catch (Exception err) { Object erro = err; m_cls_ter_tratadorErro.trataErro(ref erro); } }
private void _Check_ZIP(string pathFileZip, IDTSComponentEvents componentEvents) { bool b = false; if (_testarchive) { using (ICSharpCode.SharpZipLib.Zip.ZipInputStream fz = new ICSharpCode.SharpZipLib.Zip.ZipInputStream(System.IO.File.OpenRead(pathFileZip))) { if (!string.IsNullOrEmpty(_password)) { fz.Password = _password; } try { ICSharpCode.SharpZipLib.Zip.ZipEntry ze = fz.GetNextEntry(); componentEvents.FireInformation(1, "UnZip SSIS", "Start verify file ZIP (" + _folderSource + _fileZip + ")", null, 0, ref b); while (ze != null) { componentEvents.FireInformation(1, "UnZip SSIS", "Verifying Entry: " + ze.Name, null, 0, ref b); fz.CopyTo(System.IO.MemoryStream.Null); fz.Flush(); fz.CloseEntry(); ze = fz.GetNextEntry(); } componentEvents.FireInformation(1, "UnZip SSIS", "File ZIP verified ZIP (" + _folderSource + _fileZip + ")", null, 0, ref b); } catch (Exception ex) { throw new Exception("Verify file: " + _fileZip + " failed. (" + ex.Message + ")"); } finally { fz.Close(); } } } }
/// <summary> /// 解压缩—结果不包含文件夹 /// </summary> /// <param name="zipFilePath">源压缩的文件路径</param> /// <param name="savePath">解压后的文件保存路径</param> public static void ZipUnFileWithOutFolderInfo(string zipFilePath, string savePath) { try { using (var zipInputStream = new ICSharpCode.SharpZipLib.Zip.ZipInputStream(File.OpenRead(zipFilePath))) { ICSharpCode.SharpZipLib.Zip.ZipEntry zipEntry; while ((zipEntry = zipInputStream.GetNextEntry()) != null) { string fileName = Path.GetFileName(zipEntry.Name); if (fileName != string.Empty) { using (var fileStream = File.Create(savePath)) { int size = 2084; //初始化字节数为2兆,后面根据需要解压的内容扩展字节数 byte[] data = new byte[2048]; while (true) { size = zipInputStream.Read(data, 0, data.Length); if (size > 0) { fileStream.Write(data, 0, size); } else { break; } } fileStream.Close(); } } } zipInputStream.Close(); } } catch (Exception exception) { throw new Exception("解压缩出现错误了,错误原因:" + exception.Message); } }
protected virtual void DeserializeHieghtmap(Map map, Stream stream) { // Heightmap serialization method 3 var d = new ICSharpCode.SharpZipLib.Zip.ZipInputStream(stream); d.GetNextEntry(); var br = new BinaryReader(d); var height = br.ReadInt32(); var width = br.ReadInt32(); var hm = new Graphics.Software.Texel.R32F[height, width]; for (var y = 0; y < height; y++) { for (var x = 0; x < width; x++) { hm[y, x] = new Graphics.Software.Texel.R32F(br.ReadSingle()); } } map.Ground.Heightmap = hm; d.Close(); }
public static void ZipList(string zipPath) { var fs = new FileStream(zipPath, FileMode.Open, FileAccess.Read); var zis = new ICSharpCode.SharpZipLib.Zip.ZipInputStream(fs); while (true) { // ZipEntryを取得 var ze = zis.GetNextEntry(); if (ze == null) { break; } if (ze.IsFile) { // ファイルのとき Console.WriteLine("名前 : {0}", ze.Name); Console.WriteLine("サイズ : {0} bytes", ze.Size); Console.WriteLine("格納サイズ : {0} bytes", ze.CompressedSize); Console.WriteLine("圧縮方法 : {0}", ze.CompressionMethod); Console.WriteLine("CRC : {0:X}", ze.Crc); Console.WriteLine("日時 : {0}", ze.DateTime); Console.WriteLine(); } else if (ze.IsDirectory) { // ディレクトリのとき Console.WriteLine("ディレクトリ名 : {0}", ze.Name); Console.WriteLine("日時 : {0}", ze.DateTime); Console.WriteLine(); } } zis.Close(); fs.Close(); }
public void OnEnteredStateImpl() { FileStream zipFileRead = new FileStream(this.extractMe, FileMode.Open, FileAccess.Read, FileShare.Read); FileStream exeFileWrite = null; try { ICSharpCode.SharpZipLib.Zip.ZipInputStream zipStream = new ICSharpCode.SharpZipLib.Zip.ZipInputStream(zipFileRead); // Search for the first .msi file in the exe, and extract it ICSharpCode.SharpZipLib.Zip.ZipEntry zipEntry; bool foundExe = false; while (true) { zipEntry = zipStream.GetNextEntry(); if (zipEntry == null) { break; } if (!zipEntry.IsDirectory && string.Compare(".exe", Path.GetExtension(zipEntry.Name), true, CultureInfo.InvariantCulture) == 0) { foundExe = true; break; } } if (!foundExe) { this.exception = new FileNotFoundException(); StateMachine.QueueInput(PrivateInput.GoToError); } else { int maxBytes = (int)zipEntry.Size; int bytesSoFar = 0; this.installerPath = Path.Combine(Path.GetDirectoryName(this.extractMe), zipEntry.Name); exeFileWrite = new FileStream(this.installerPath, FileMode.Create, FileAccess.Write, FileShare.Read); SiphonStream siphonStream2 = new SiphonStream(exeFileWrite, 4096); this.abortMeStream = siphonStream2; IOEventHandler ioFinishedDelegate = delegate(object sender, IOEventArgs e) { bytesSoFar += e.Count; double percent = 100.0 * ((double)bytesSoFar / (double)maxBytes); OnProgress(percent); }; OnProgress(0.0); if (maxBytes > 0) { siphonStream2.IOFinished += ioFinishedDelegate; } Utility.CopyStream(zipStream, siphonStream2); if (maxBytes > 0) { siphonStream2.IOFinished -= ioFinishedDelegate; } this.abortMeStream = null; siphonStream2 = null; exeFileWrite.Close(); exeFileWrite = null; zipStream.Close(); zipStream = null; StateMachine.QueueInput(PrivateInput.GoToReadyToInstall); } } catch (Exception ex) { if (this.AbortRequested) { StateMachine.QueueInput(PrivateInput.GoToAborted); } else { this.exception = ex; StateMachine.QueueInput(PrivateInput.GoToError); } } finally { if (exeFileWrite != null) { exeFileWrite.Close(); exeFileWrite = null; } if (zipFileRead != null) { zipFileRead.Close(); zipFileRead = null; } if (this.exception != null || this.AbortRequested) { if (this.installerPath != null) { try { File.Delete(this.installerPath); } catch { } } } if (this.extractMe != null) { try { File.Delete(this.extractMe); } catch { } } } }
public void OnEnteredStateImpl() { FileStream zipFileRead = new FileStream(this.extractMe, FileMode.Open, FileAccess.Read, FileShare.Read); FileStream exeFileWrite = null; try { ICSharpCode.SharpZipLib.Zip.ZipInputStream zipStream = new ICSharpCode.SharpZipLib.Zip.ZipInputStream(zipFileRead); // Search for the first .msi file in the exe, and extract it ICSharpCode.SharpZipLib.Zip.ZipEntry zipEntry; bool foundExe = false; while (true) { zipEntry = zipStream.GetNextEntry(); if (zipEntry == null) { break; } if (!zipEntry.IsDirectory && string.Compare(".exe", Path.GetExtension(zipEntry.Name), true, CultureInfo.InvariantCulture) == 0) { foundExe = true; break; } } if (!foundExe) { this.exception = new FileNotFoundException(); StateMachine.QueueInput(PrivateInput.GoToError); } else { int maxBytes = (int)zipEntry.Size; int bytesSoFar = 0; this.installerPath = Path.Combine(Path.GetDirectoryName(this.extractMe), zipEntry.Name); exeFileWrite = new FileStream(this.installerPath, FileMode.Create, FileAccess.Write, FileShare.Read); SiphonStream siphonStream2 = new SiphonStream(exeFileWrite, 4096); this.abortMeStream = siphonStream2; IOEventHandler ioFinishedDelegate = delegate(object sender, IOEventArgs e) { bytesSoFar += e.Count; double percent = 100.0 * ((double)bytesSoFar / (double)maxBytes); OnProgress(percent); }; OnProgress(0.0); if (maxBytes > 0) { siphonStream2.IOFinished += ioFinishedDelegate; } Utility.CopyStream(zipStream, siphonStream2); if (maxBytes > 0) { siphonStream2.IOFinished -= ioFinishedDelegate; } this.abortMeStream = null; siphonStream2 = null; exeFileWrite.Close(); exeFileWrite = null; zipStream.Close(); zipStream = null; StateMachine.QueueInput(PrivateInput.GoToReadyToInstall); } } catch (Exception ex) { if (this.AbortRequested) { StateMachine.QueueInput(PrivateInput.GoToAborted); } else { this.exception = ex; StateMachine.QueueInput(PrivateInput.GoToError); } } finally { if (exeFileWrite != null) { exeFileWrite.Close(); exeFileWrite = null; } if (zipFileRead != null) { zipFileRead.Close(); zipFileRead = null; } if (this.exception != null || this.AbortRequested) { if (this.installerPath != null) { bool result = FileSystem.TryDeleteFile(this.installerPath); } } if (this.extractMe != null) { bool result = FileSystem.TryDeleteFile(this.extractMe); } } }
protected virtual void DeserializeHieghtmap(Map map, Stream stream) { // Heightmap serialization method 3 var d = new ICSharpCode.SharpZipLib.Zip.ZipInputStream(stream); d.GetNextEntry(); var br = new BinaryReader(d); var height = br.ReadInt32(); var width = br.ReadInt32(); var hm = new Graphics.Software.Texel.R32F[height, width]; for (var y = 0; y < height; y++) for (var x = 0; x < width; x++) hm[y, x] = new Graphics.Software.Texel.R32F(br.ReadSingle()); map.Ground.Heightmap = hm; d.Close(); }
//directory : end of "/" or "\\" public static bool UnZipDirectory(string zipFileName, string directory, string password = null, Action <string, float, long, long> cb = null) { try { /*if (!Directory.Exists(directory)) * Directory.CreateDirectory(directory); * directory = directory.Replace('\\', '/'); * if (directory.EndsWith("/")) * directory = directory.Substring(0, directory.Length - 1); * SharpZipLib.Zip.FastZipEvents events = new SharpZipLib.Zip.FastZipEvents(); * events.Progress += (object sender, SharpZipLib.Core.ProgressEventArgs e) => * { * if(cb != null) * { * cb(e.Name, e.PercentComplete, e.Processed, e.Target); * } * else * { * LogUtil.Log("UnZip {0} {1}/{2}--{3}",e.Name, e.Processed, e.Target,e.PercentComplete); * } * }; * SharpZipLib.Zip.FastZip fz = new SharpZipLib.Zip.FastZip(events); * fz.Password = password; * fz.ExtractZip(zipFileName,directory, SharpZipLib.Zip.FastZip.Overwrite.Always,null,null,null,true); */ if (!Directory.Exists(directory)) { Directory.CreateDirectory(directory); } SharpZipLib.Zip.ZipFile szip = new SharpZipLib.Zip.ZipFile(zipFileName); szip.Password = password; long count = szip.Count; szip.Close(); SharpZipLib.Zip.ZipInputStream s = new SharpZipLib.Zip.ZipInputStream(File.OpenRead(zipFileName)); s.Password = password; SharpZipLib.Zip.ZipEntry theEntry; int n = 0; while ((theEntry = s.GetNextEntry()) != null) { string directoryName = Path.GetDirectoryName(theEntry.Name); string fileName = Path.GetFileName(theEntry.Name); if (directoryName != string.Empty) { Directory.CreateDirectory(Path.Combine(directory, directoryName)); } if (fileName != string.Empty) { FileStream streamWriter = File.Create(Path.Combine(directory, theEntry.Name)); LogUtil.Log("-------------->Begin UnZip {0},Size:{1}", theEntry.Name, theEntry.Size); 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; } if (cb != null) { cb(theEntry.Name, theEntry.Size > 0 ? 100 * streamWriter.Length * 1.0f / theEntry.Size : 100, streamWriter.Length, theEntry.Size); } // else // { // LogUtil.Log("UnZip {0} {1}/{2}--{3}", theEntry.Name, streamWriter.Length, theEntry.Size, theEntry.Size > 0 ? 100*streamWriter.Length*1.0f / theEntry.Size : 100); // } } streamWriter.Close(); n++; LogUtil.Log("-------------->End UnZip {0}, {1}/{2}", theEntry.Name, n, count); } } s.Close(); return(true); } catch (Exception e) { LogUtil.LogError(e.Message); return(false); } }
/// <summary> /// Extractor function /// </summary> /// <param name="zipFilename">zipFilename</param> /// <param name="ExtractDir">ExtractDir</param> /// <param name="deleteZippedfile">if true, zipped file will be deleted after extraction.</param> public void ExtractArchive(string zipFilename, string ExtractDir, bool deleteZippedfile) { // int Redo = 1; ICSharpCode.SharpZipLib.Zip.ZipInputStream MyZipInputStream; FileStream MyFileStream; MyZipInputStream = new ICSharpCode.SharpZipLib.Zip.ZipInputStream(new FileStream(zipFilename, FileMode.Open, FileAccess.Read)); ICSharpCode.SharpZipLib.Zip.ZipEntry MyZipEntry = MyZipInputStream.GetNextEntry(); Directory.CreateDirectory(ExtractDir); while (!(MyZipEntry == null)) { if (MyZipEntry.IsDirectory) { Directory.CreateDirectory(ExtractDir + "\\" + MyZipEntry.Name); } else { if (!Directory.Exists(ExtractDir + "\\" + Path.GetDirectoryName(MyZipEntry.Name))) { Directory.CreateDirectory(ExtractDir + "\\" + Path.GetDirectoryName(MyZipEntry.Name)); } MyFileStream = new FileStream(ExtractDir + "\\" + MyZipEntry.Name, FileMode.OpenOrCreate, FileAccess.Write); int count; byte[] buffer = new byte[4096]; count = MyZipInputStream.Read(buffer, 0, 4096); while (count > 0) { MyFileStream.Write(buffer, 0, count); count = MyZipInputStream.Read(buffer, 0, 4096); } MyFileStream.Close(); } try { MyZipEntry = MyZipInputStream.GetNextEntry(); } catch (Exception ex) { MyZipEntry = null; } } //dispose active objects try { if (!(MyZipInputStream == null)) MyZipInputStream.Close(); } catch (Exception ex) { } finally { if (deleteZippedfile) { //delete the zip file if (System.IO.File.Exists(zipFilename)) System.IO.File.Delete(zipFilename); } } }
public static bool UnZipFile(string inputPathOfZipFile, out string returnMsg) { returnMsg = string.Empty; bool ret = true; try { if (File.Exists(inputPathOfZipFile)) { string baseDirectory = Path.GetDirectoryName(inputPathOfZipFile); using (ICSharpCode.SharpZipLib.Zip.ZipInputStream zipStream = new ICSharpCode.SharpZipLib.Zip.ZipInputStream(File.OpenRead(inputPathOfZipFile))) { ICSharpCode.SharpZipLib.Zip.ZipEntry theEntry; while ((theEntry = zipStream.GetNextEntry()) != null) { if (theEntry.IsFile) { if (theEntry.Name != "") { string strNewFile = @"" + baseDirectory + @"\" + theEntry.Name; //Check thư mục chứa nó, Nếu không có thì tạo mới string folder = Path.GetDirectoryName(strNewFile); if (folder != null && !Directory.Exists(folder)) { Directory.CreateDirectory(folder); } ////////End using (FileStream streamWriter = File.Create(strNewFile)) { byte[] data = new byte[2048]; while (true) { int size = zipStream.Read(data, 0, data.Length); if (size > 0) { streamWriter.Write(data, 0, size); } else { break; } } streamWriter.Close(); } } } else if (theEntry.IsDirectory) { string strNewDirectory = @"" + baseDirectory + @"\" + theEntry.Name; if (!Directory.Exists(strNewDirectory)) { Directory.CreateDirectory(strNewDirectory); } } } zipStream.Close(); } } } catch (Exception ex) { ret = false; returnMsg = ex.Message + "\r\n\r\n" + ex.StackTrace; } return(ret); }
private void _DeCompressZIP(string pathFileZip, IDTSComponentEvents componentEvents) { bool b = false; _Check_ZIP(pathFileZip, componentEvents); using (ICSharpCode.SharpZipLib.Zip.ZipInputStream fz = new ICSharpCode.SharpZipLib.Zip.ZipInputStream(System.IO.File.OpenRead(pathFileZip))) { if (!string.IsNullOrEmpty(_password)) { fz.Password = _password; } ICSharpCode.SharpZipLib.Zip.ZipEntry ze = fz.GetNextEntry(); while (ze != null) { string fn = ze.Name.Replace("/", "\\"); System.IO.FileInfo fi = null; if ((!System.Text.RegularExpressions.Regex.Match(fn, _fileFilter).Success) || (ze.IsDirectory && ze.Size == 0)) { if (!System.Text.RegularExpressions.Regex.Match(fn, _fileFilter).Success) { componentEvents.FireInformation(1, "UnZip SSIS", _typeCompression.ToString() + ": file " + fn + " doesn't match regex filter '" + _fileFilter + "'", null, 0, ref b); // Added information display when regex doesn't match (Updated on 2015-12-30 by Nico_FR75) } fz.CloseEntry(); ze = fz.GetNextEntry(); continue; } componentEvents.FireInformation(1, "UnZip SSIS", _typeCompression.ToString() + ": De-Compress (with '" + _storePaths.ToString() + "') file: " + fn, null, 0, ref b); if (_storePaths == Store_Paths.Absolute_Paths || _storePaths == Store_Paths.Relative_Paths) { //Absolute / Relative Path fi = new System.IO.FileInfo(_folderDest + fn); if (!System.IO.Directory.Exists(fi.DirectoryName)) { System.IO.Directory.CreateDirectory(fi.DirectoryName); } } else if (_storePaths == Store_Paths.No_Paths) { //No Path fi = new System.IO.FileInfo(_folderDest + System.IO.Path.GetFileName(fn)); } else { throw new Exception("Please select type Store Paths (No_Paths / Relative_Paths / Absolute_Paths)."); } using (System.IO.FileStream fs = new System.IO.FileStream(fi.FullName, System.IO.FileMode.Create, System.IO.FileAccess.Write)) { fz.CopyTo(fs); fs.Flush(); } fz.CloseEntry(); ze = fz.GetNextEntry(); } fz.Close(); } }
// Uncomment these cases & run them on an older Lucene // version, to generate an index to test backwards // compatibility. Then, cd to build/test/index.cfs and // run "zip index.<VERSION>.cfs.zip *"; cd to // build/test/index.nocfs and run "zip // index.<VERSION>.nocfs.zip *". Then move those 2 zip // files to your trunk checkout and add them to the // oldNames array. /* public void testCreatePreLocklessCFS() throws IOException { createIndex("index.cfs", true); } public void testCreatePreLocklessNoCFS() throws IOException { createIndex("index.nocfs", false); } */ /* Unzips dirName + ".zip" --> dirName, removing dirName first */ public virtual void Unzip(System.String zipName, System.String destDirName) { #if SHARP_ZIP_LIB // get zip input stream ICSharpCode.SharpZipLib.Zip.ZipInputStream zipFile; zipFile = new ICSharpCode.SharpZipLib.Zip.ZipInputStream(System.IO.File.OpenRead(zipName + ".zip")); // get dest directory name System.String dirName = FullDir(destDirName); System.IO.FileInfo fileDir = new System.IO.FileInfo(dirName); // clean up old directory (if there) and create new directory RmDir(fileDir.FullName); System.IO.Directory.CreateDirectory(fileDir.FullName); // copy file entries from zip stream to directory ICSharpCode.SharpZipLib.Zip.ZipEntry entry; while ((entry = zipFile.GetNextEntry()) != null) { System.IO.Stream streamout = new System.IO.BufferedStream(new System.IO.FileStream(new System.IO.FileInfo(System.IO.Path.Combine(fileDir.FullName, entry.Name)).FullName, System.IO.FileMode.Create)); byte[] buffer = new byte[8192]; int len; while ((len = zipFile.Read(buffer, 0, buffer.Length)) > 0) { streamout.Write(buffer, 0, len); } streamout.Close(); } zipFile.Close(); #else Assert.Fail("Needs integration with SharpZipLib"); #endif }
/// <summary> /// 解压功能(解压压缩文件到指定目录) /// </summary> /// <param name="FileToUpZip">待解压的文件</param> /// <param name="ZipedFolder">指定解压目标目录</param> public static void UnZip(string FileToUpZip, string ZipedFolder) { if (!File.Exists(FileToUpZip)) { return; } if (!Directory.Exists(ZipedFolder)) { Directory.CreateDirectory(ZipedFolder); } ICSharpCode.SharpZipLib.Zip.ZipInputStream s = null; ICSharpCode.SharpZipLib.Zip.ZipEntry theEntry = null; string fileName; FileStream streamWriter = null; try { s = new ICSharpCode.SharpZipLib.Zip.ZipInputStream(File.OpenRead(FileToUpZip)); while ((theEntry = s.GetNextEntry()) != null) { if (theEntry.Name != String.Empty) { fileName = Path.Combine(ZipedFolder, theEntry.Name); ///判断文件路径是否是文件夹 if (fileName.EndsWith("/") || fileName.EndsWith("\\")) { Directory.CreateDirectory(fileName); continue; } streamWriter = File.Create(fileName); 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; } } } } } finally { if (streamWriter != null) { streamWriter.Close(); streamWriter = null; } if (theEntry != null) { theEntry = null; } if (s != null) { s.Close(); s = null; } GC.Collect(); GC.Collect(1); } }