public void Decompress(string OutputFileName, string ZlibFileName) { System.IO.FileStream raw = System.IO.File.OpenRead(ZlibFileName); ICSharpCode.SharpZipLib.Zip.ZipInputStream Decompressor = new ICSharpCode.SharpZipLib.Zip.ZipInputStream(raw); Decompressor.GetNextEntry(); System.IO.FileStream output = System.IO.File.Create(OutputFileName); byte[] buf = new byte[1024]; int n = 0; while (true) { n = Decompressor.Read(buf, 0, buf.Length); if (n == 0) { break; } else { output.Write(buf, 0, n); } } output.Close(); Decompressor.Close(); raw.Close(); //delete(Decompressor); //delete(buf); }
public static void Main(string[] args) { ZipInputStream s = new ZipInputStream(File.OpenRead(args[0])); ZipEntry theEntry; while ((theEntry = s.GetNextEntry()) != null) { Console.WriteLine(theEntry.Name); string directoryName = Path.GetDirectoryName(theEntry.Name); string fileName = Path.GetFileName(theEntry.Name); // create directory Directory.CreateDirectory(directoryName); if (fileName != String.Empty) { FileStream streamWriter = File.Create(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(); }
/// ------------------------------------------------------------------------------------ /// <summary> /// Unzip a byte array and return unpacked data /// </summary> /// <param name="data">Byte array containing data to be unzipped</param> /// <returns>unpacked data</returns> /// ------------------------------------------------------------------------------------ public static byte[] UnpackData(byte[] data) { if (data == null) return null; try { MemoryStream memStream = new MemoryStream(data); ZipInputStream zipStream = new ZipInputStream(memStream); zipStream.GetNextEntry(); MemoryStream streamWriter = new MemoryStream(); int size = 2048; byte[] dat = new byte[2048]; while (true) { size = zipStream.Read(dat, 0, dat.Length); if (size > 0) { streamWriter.Write(dat, 0, size); } else { break; } } streamWriter.Close(); zipStream.CloseEntry();
public static void UnZip(this FileInfo fileInfo, string destiantionFolder) { using (var fileStreamIn = new FileStream(fileInfo.FullName, FileMode.Open, FileAccess.Read)) { using (var zipInStream = new ZipInputStream(fileStreamIn)) { var entry = zipInStream.GetNextEntry(); FileStream fileStreamOut = null; while (entry != null) { fileStreamOut = new FileStream(destiantionFolder + @"\" + entry.Name, FileMode.Create, FileAccess.Write); int size; byte[] buffer = new byte[4096]; do { size = zipInStream.Read(buffer, 0, buffer.Length); fileStreamOut.Write(buffer, 0, size); } while (size > 0); fileStreamOut.Close(); entry = zipInStream.GetNextEntry(); } if (fileStreamOut != null) fileStreamOut.Close(); zipInStream.Close(); } fileStreamIn.Close(); } }
public Block DeserializeBlockFromZippedXML(String zipFilePath, String blockFileName) { FileStream zipFileStream = new FileStream(zipFilePath, FileMode.Open); ZipInputStream zippedStream = new ZipInputStream(zipFileStream); ZipEntry zipEntry; Block block = null; while ((zipEntry = zippedStream.GetNextEntry()) != null) { if (zipEntry.Name == blockFileName) { block = (Block)xmls.Deserialize(zippedStream); break; } } zippedStream.Close(); // the following is a workaround. We took a lot of data before we started recording // the rfState. Luckily, almost all of this data was taken in the same rf state. // Here, if no rfState is found in the block it is assigned the default, true. try { bool rfState = (bool)block.Config.Settings["rfState"]; } catch (Exception) { block.Config.Settings.Add("rfState", true); } return block; }
public override Stream GetInputStream(UploadedFile file) { FileStream fileS = null; ZipInputStream zipS = null; try { string path = GetZipPath(file); fileS = File.OpenRead(path); zipS = new ZipInputStream(fileS); zipS.GetNextEntry(); return zipS; } catch { if (fileS != null) fileS.Close(); if (zipS != null) zipS.Close(); return null; } }
private void ParseZipAsync(byte[] zipFileData, Action <byte[]> callback) { Loom.RunAsync(() => { MemoryStream stream = new MemoryStream(zipFileData); stream.Seek(0, SeekOrigin.Begin); ICSharpCode.SharpZipLib.Zip.ZipInputStream zipStream = new ICSharpCode.SharpZipLib.Zip.ZipInputStream(stream); for (ICSharpCode.SharpZipLib.Zip.ZipEntry theEntry = zipStream.GetNextEntry(); theEntry != null; theEntry = zipStream.GetNextEntry()) { if (theEntry.IsFile == false) { continue; } if (theEntry.Name.EndsWith(".meta")) { continue; } string fileName = Path.GetFileName(theEntry.Name); if (fileName != String.Empty) { List <byte> result = new List <byte>(); byte[] data = new byte[2048]; while (true) { int size = zipStream.Read(data, 0, data.Length); if (size > 0) { var bytes = new Byte[size]; Array.Copy(data, bytes, size); result.AddRange(bytes); } else { break; } } //文件名都转为小写 if (m_DictLuaScriptData.ContainsKey(fileName.ToLower())) { string str = string.Format("ResourcesManager.InitZip:Zip中文件名{0}重复", fileName); Debug.LogError(str); continue; } m_DictLuaScriptData.Add(theEntry.Name.ToLower(), result.ToArray()); } } zipStream.Close(); stream.Close(); Loom.QueueOnMainThread(() => { callback(zipFileData); }); }); }
/// <summary> /// 解压缩 /// </summary> /// <param name="zipBytes">待解压数据</param> /// <returns></returns> public static byte[] UnZip(byte[] zipBytes) { ICSharpCode.SharpZipLib.Zip.ZipInputStream zipStream = null; ICSharpCode.SharpZipLib.Zip.ZipEntry ent = null; byte[] reslutBytes = null; try { using (System.IO.MemoryStream inputZipStream = new System.IO.MemoryStream(zipBytes)) { zipStream = new ICSharpCode.SharpZipLib.Zip.ZipInputStream(inputZipStream); if ((ent = zipStream.GetNextEntry()) != null) { reslutBytes = new byte[zipStream.Length]; zipStream.Read(reslutBytes, 0, reslutBytes.Length); } } } finally { if (zipStream != null) { zipStream.Close(); zipStream.Dispose(); } if (ent != null) { ent = null; } GC.Collect(); GC.Collect(1); } return(reslutBytes); }
public void Extract(string path, string dest_dir) { ZipInputStream zipstream = new ZipInputStream(new FileStream( path, FileMode.Open)); ZipEntry theEntry; while ((theEntry = zipstream.GetNextEntry()) != null) { string directoryName = Path.GetDirectoryName(theEntry.Name); string fileName = Path.GetFileName(theEntry.Name); // create directory if (directoryName != String.Empty) Directory.CreateDirectory(directoryName); if (fileName != String.Empty) { FileStream streamWriter = File.Create( Path.Combine( dest_dir, theEntry.Name) ); int size = 0; byte[] buffer = new byte[2048]; while ((size = zipstream.Read(buffer, 0, buffer.Length)) > 0) { streamWriter.Write(buffer, 0, size); } streamWriter.Close(); } } zipstream.Close(); }
static public long GetDeCompressFileSize(string zipPath) { long res = 0; System.IO.FileStream fs = new System.IO.FileStream(zipPath, System.IO.FileMode.Open, System.IO.FileAccess.Read, System.IO.FileShare.Read); ICSharpCode.SharpZipLib.Zip.ZipInputStream zis = new ICSharpCode.SharpZipLib.Zip.ZipInputStream(fs); try { ICSharpCode.SharpZipLib.Zip.ZipEntry ze; while ((ze = zis.GetNextEntry()) != null) { if (!ze.IsDirectory) { res += ze.Size; } } } catch (Exception e) { Debug.Log(e); res = 0; } zis.Close(); fs.Close(); return(res); }
public void UnZipFile(string ZipedFile, string UnZipFile) { ZipInputStream zipInputStream = new ZipInputStream(File.OpenRead(ZipedFile)); ZipEntry nextEntry; while ((nextEntry = zipInputStream.GetNextEntry()) != null) { string directoryName = Path.GetDirectoryName(UnZipFile); string fileName = Path.GetFileName(nextEntry.Name); Directory.CreateDirectory(directoryName); if (fileName != string.Empty) { FileStream fileStream = File.OpenWrite(UnZipFile); byte[] array = new byte[2048]; while (true) { int num = zipInputStream.Read(array, 0, array.Length); if (num <= 0) { break; } fileStream.Write(array, 0, num); } fileStream.Close(); } } zipInputStream.Close(); }
public static void UnZipFile(string[] args) { ZipInputStream zipInputStream = new ZipInputStream((Stream) File.OpenRead(args[0].Trim())); string directoryName = Path.GetDirectoryName(args[1].Trim()); if (!Directory.Exists(args[1].Trim())) Directory.CreateDirectory(directoryName); ZipEntry nextEntry; while ((nextEntry = zipInputStream.GetNextEntry()) != null) { string fileName = Path.GetFileName(nextEntry.Name); if (fileName != string.Empty) { FileStream fileStream = File.Create(args[1].Trim() + fileName); byte[] buffer = new byte[2048]; while (true) { int count = zipInputStream.Read(buffer, 0, buffer.Length); if (count > 0) fileStream.Write(buffer, 0, count); else break; } fileStream.Close(); } } zipInputStream.Close(); }
public static string Unzip(string fileNameZip) { string path = Path.GetDirectoryName(fileNameZip); string folderName = Path.Combine(path, Path.GetFileNameWithoutExtension(fileNameZip)); CreateDirectory(folderName); ZipInputStream strmZipInputStream = new ZipInputStream(File.OpenRead(fileNameZip)); ZipEntry entry; while ((entry = strmZipInputStream.GetNextEntry()) != null) { FileStream fs = File.Create(Path.Combine(folderName, entry.Name)); int size = 2048; byte[] data = new byte[2048]; while (true) { size = strmZipInputStream.Read(data, 0, data.Length); if (size > 0) fs.Write(data, 0, size); else break; } fs.Close(); } strmZipInputStream.Close(); return folderName; }
/// <summary> /// 解压缩文件 /// </summary> /// <param name="输入压缩文件路径">要解压的压缩文件</param> /// <param name="解压缩目录">解压目标目录路径</param> public static void 解压缩(string 输入压缩文件路径, string 解压缩目录) { ZipInputStream inputStream = new ZipInputStream(File.OpenRead(输入压缩文件路径)); ZipEntry theEntry; while ((theEntry = inputStream.GetNextEntry()) != null) { 解压缩目录 += "/"; string fileName = Path.GetFileName(theEntry.Name); string path = Path.GetDirectoryName(解压缩目录 + theEntry.Name) + "/"; Directory.CreateDirectory(path);//生成解压目录 if (fileName != String.Empty) { FileStream streamWriter = File.Create(path + fileName);//解压文件到指定的目录 int size = 2048; byte[] data = new byte[2048]; while (true) { size = inputStream.Read(data, 0, data.Length); if (size > 0) { streamWriter.Write(data, 0, size); } else { break; } } streamWriter.Close(); } } inputStream.Close(); }
public void Decompress(System.IO.Stream OutputStream, System.IO.Stream InputStream) { ICSharpCode.SharpZipLib.Zip.ZipInputStream Decompressor = new ICSharpCode.SharpZipLib.Zip.ZipInputStream(InputStream); Decompressor.GetNextEntry(); byte[] buf = new byte[1024]; int n = 0; while (true) { n = Decompressor.Read(buf, 0, buf.Length); if (n == 0) { break; } else { OutputStream.Write(buf, 0, n); } } OutputStream.Seek(0, System.IO.SeekOrigin.Begin); Decompressor.Close(); //delete(Decompressor); //delete(buf); }
public static void Main(string[] args) { ZipInputStream s = new ZipInputStream(File.OpenRead(args[0])); ZipEntry theEntry; while ((theEntry = s.GetNextEntry()) != null) { Console.WriteLine("Name : {0}", theEntry.Name); Console.WriteLine("Date : {0}", theEntry.DateTime); Console.WriteLine("Size : (-1, if the size information is in the footer)"); Console.WriteLine(" Uncompressed : {0}", theEntry.Size); Console.WriteLine(" Compressed : {0}", theEntry.CompressedSize); int size = 2048; byte[] data = new byte[2048]; Console.Write("Show Entry (y/n) ?"); if (Console.ReadLine() == "y") { // System.IO.Stream st = File.Create("G:\\a.tst"); while (true) { size = s.Read(data, 0, data.Length); // st.Write(data, 0, size); if (size > 0) { Console.Write(new ASCIIEncoding().GetString(data, 0, size)); } else { break; } } // st.Close(); } Console.WriteLine(); } s.Close(); }
public KarFile(string fileName, ReportProgressFunction ReportProgress) { _fileName = fileName; _directory = FileFunctions.CreateTempoaryDirectory(); using (FileStream file = new FileStream(fileName, FileMode.Open, FileAccess.Read)) using (ZipInputStream zip = new ZipInputStream(file)) { while (true) { ZipEntry item = zip.GetNextEntry(); if (item == null) { break; } string itemDirectory = Path.GetDirectoryName(item.Name); string itemName = Path.GetFileName(item.Name); string outputDirectory = Path.Combine(_directory, itemDirectory); FileFunctions.CreateDirectory(outputDirectory, ReportProgress); string outputName = Path.Combine(outputDirectory, itemName); StreamFunctions.ExportUntilFail(outputName, zip, ReportProgress); } zip.Close(); } }
/// <summary> /// 解压缩文件 /// </summary> /// <param name="zipPath">源文件</param> /// <param name="directory">目标文件</param> /// <param name="password">密码</param> public void File(string zipPath, string directory, string password = null) { FileInfo objFile = new FileInfo(zipPath); if (!objFile.Exists || !objFile.Extension.ToUpper().Equals(".ZIP")) return; FileDirectory.DirectoryCreate(directory); ZipInputStream objZIS = new ZipInputStream(System.IO.File.OpenRead(zipPath)); if (!password.IsNullEmpty()) objZIS.Password = password; ZipEntry objEntry; while ((objEntry = objZIS.GetNextEntry()) != null) { string directoryName = Path.GetDirectoryName(objEntry.Name); string fileName = Path.GetFileName(objEntry.Name); if (directoryName != String.Empty) FileDirectory.DirectoryCreate(directory + directoryName); if (fileName != String.Empty) { FileStream streamWriter = System.IO.File.Create(Path.Combine(directory, objEntry.Name)); int size = 2048; byte[] data = new byte[2048]; while (true) { size = objZIS.Read(data, 0, data.Length); if (size > 0) { streamWriter.Write(data, 0, size); } else { break; } } streamWriter.Close(); } } objZIS.Close(); }
public static bool UnZipFile(string InputPathOfZipFile, string NewName) { bool ret = true; try { if (File.Exists(InputPathOfZipFile)) { string baseDirectory = Path.GetDirectoryName(InputPathOfZipFile); using (ZipInputStream ZipStream = new ZipInputStream(File.OpenRead(InputPathOfZipFile))) { ZipEntry theEntry; while ((theEntry = ZipStream.GetNextEntry()) != null) { if (theEntry.IsFile) { if (theEntry.Name != "") { string strNewFile = @"" + baseDirectory + @"\" + NewName; if (File.Exists(strNewFile)) { //continue; } using (FileStream streamWriter = File.Create(strNewFile)) { int size = 2048; byte[] data = new byte[2048]; while (true) { 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; } return ret; }
public static bool UnZipFile(string zipPathAndFile, string outputFolder, string password, bool deleteZipFile) { try { ZipInputStream s = new ZipInputStream(File.OpenRead(zipPathAndFile)); if (password != null && password != String.Empty) s.Password = password; ZipEntry theEntry; string tmpEntry = String.Empty; while ((theEntry = s.GetNextEntry()) != null) { string directoryName = outputFolder; string fileName = Path.GetFileName(theEntry.Name); if (directoryName != "") { //if(Directory.Exists( Directory.CreateDirectory(directoryName); } if (fileName != String.Empty) { if (theEntry.Name.IndexOf(".ini") < 0) { string fullPath = directoryName + "\\" + theEntry.Name; fullPath = fullPath.Replace("\\ ", "\\"); string fullDirPath = Path.GetDirectoryName(fullPath); if (!Directory.Exists(fullDirPath)) Directory.CreateDirectory(fullDirPath); FileStream streamWriter = File.Create(fullPath); 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.Flush(); streamWriter.Close(); } } } //s.Flush(); s.Close(); if (deleteZipFile) File.Delete(zipPathAndFile); return true; } catch (Exception e) { Console.WriteLine(e.StackTrace); return false; } }
//解压缩 /// <summary> /// 解压缩文件 /// </summary> /// <param name="fileName">要解压的文件</param> /// <param name="folder">解压的路径</param> public static void UnZipFile(string fileName, string folder) { if (!Directory.Exists(folder)) { Directory.CreateDirectory(folder); //创建解压的文件夹 } ZipInputStream f = new ZipInputStream(File.OpenRead(fileName)); //读取压缩文件,并用此文件流新建 “ZipInputStream”对象 A: ZipEntry zp = f.GetNextEntry(); //获取解压文件流中的项目。 另注(我的理解):在压缩包里每个文件都以“ZipEntry”形式存在,其中包括存放文件的目录信息。如果空目录被压缩,该目录下将出现一个名称为空、大小为 0 、“Crc”属性为 00000000 的“文件”。此文件只是个标记,不会被解压。 while (zp != null) { string un_tmp2; if (zp.IsDirectory) { if (!Directory.Exists(folder + "/" + zp.Name)) { Directory.CreateDirectory(folder + "/" + zp.Name); } } if (zp.Name.IndexOf(@"/") >= 0) //获取文件的目录信息 { int tmp1 = zp.Name.LastIndexOf("/"); un_tmp2 = zp.Name.Substring(0, tmp1); un_tmp2 = folder + "/" + un_tmp2; if (!Directory.Exists(un_tmp2)) { Directory.CreateDirectory(un_tmp2); //必须先创建目录,否则解压失败 --- (A) 关系到下面的步骤(B) } } if (!zp.IsDirectory && zp.Crc != 00000000L) //此“ZipEntry”不是“标记文件” { System.Console.WriteLine(string.Format("正在加压文件 {0}",zp.Name)); int i = 2048; byte[] b = new byte[i]; //每次缓冲 2048 字节 FileStream s = File.Create(folder + "/" + zp.Name); //(B)-新建文件流 while (true) //持续读取字节,直到一个“ZipEntry”字节读完 { i = f.Read(b, 0, b.Length); //读取“ZipEntry”中的字节 if (i > 0) { s.Write(b, 0, i); //将字节写入新建的文件流 } else { break; //读取的字节为 0 ,跳出循环 } } s.Close(); } goto A; //进入下一个“ZipEntry” } f.Close(); }
public static void UnZip(string fileName, string target) { if (!target.EndsWith("\\")) target += "\\"; using (ZipInputStream inputStream = new ZipInputStream(File.OpenRead(fileName))) { try { ZipEntry theEntry; while ((theEntry = inputStream.GetNextEntry()) != null) { if (theEntry.IsDirectory) { Directory.CreateDirectory(target + theEntry.Name); } else if (theEntry.IsFile) { using (FileStream fileWrite = new FileStream(target + theEntry.Name, FileMode.Create, FileAccess.Write)) { try { byte[] buffer = new byte[2048]; int size; while (true) { size = inputStream.Read(buffer, 0, buffer.Length); if (size > 0) fileWrite.Write(buffer, 0, size); else break; } } catch (Exception) { throw; } finally { fileWrite.Close(); } } } } } catch (Exception) { throw; } finally { inputStream.Close(); } } }
/// <summary> /// Check zip file is encrypted or not /// </summary> /// <param name="zipFileName"></param> /// <returns></returns> public static bool IsEncrypt(string zipFileName) { bool blnReturn; ZipInputStream s = new ZipInputStream(File.OpenRead(zipFileName)); try { ZipEntry theEntry = s.GetNextEntry(); blnReturn = theEntry.IsCrypted; s.Close(); return blnReturn; } catch(Exception ex) { if(s != null) { s.Close(); } throw ex; } }
public static bool UnPackFiles(string path, string filePath) { try { if (!Directory.Exists(path)) { Directory.CreateDirectory(path); } path = Path.GetDirectoryName(path); ZipInputStream zstream = new ZipInputStream(File.OpenRead(filePath)); ZipEntry entry; while ((entry = zstream.GetNextEntry()) != null) { if (!string.IsNullOrEmpty(entry.Name)) { string upath = path + "\\" + entry.Name; if (entry.IsDirectory) { Directory.CreateDirectory(upath); } else if (entry.CompressedSize > 0) { FileStream fs = File.Create(upath); int size = 2048; byte[] data = new byte[size]; while (true) { size = zstream.Read(data, 0, data.Length); if (size > 0) { fs.Write(data, 0, size); } else { break; } } fs.Dispose(); fs.Close(); } } } zstream.Dispose(); zstream.Close(); return true; } catch { throw; } }
public void UpZip() { int k = 0; ResourceManager rm = Install_Bin.ResourceManager; string rootPath = SiteInfo.Current.WebPath + "\\"; Stream stream = new MemoryStream((byte[])rm.GetObject("bbsmax_zip")); Stream zip = new MemoryStream((byte[])rm.GetObject("bbsmax_zip")); //先计算出文件个数 using (ZipInputStream zipInputStream = new ZipInputStream(stream)) { ZipEntry zipEntry; while ((zipEntry = zipInputStream.GetNextEntry()) != null) k++; zipInputStream.Close(); } using (ZipInputStream s = new ZipInputStream(zip)) { ZipEntry entry; int i = 0; while ((entry = s.GetNextEntry()) != null) { while (!this.running) { Thread.Sleep(1000); } InstallBarEvent(k, i++);//触发事件 string directoryName = Path.GetDirectoryName(entry.Name); string fileName = Path.GetFileName(entry.Name); try { if (directoryName.Length > 0 && !Directory.Exists(rootPath + directoryName)) { Directory.CreateDirectory(rootPath + directoryName); } if (!string.IsNullOrEmpty(fileName)) { string eName = entry.Name; eName = eName.ToLower(); SetupManager.CreateFile(rootPath + eName, s); } } catch (Exception ex) { SetupManager.CreateLog("解压文件时出错:" + ex.ToString()); throw ex; } } s.Close(); } }
public static ArrayList ListZipFiles(string zipPathAndFile, string password) { ArrayList arrayList = new ArrayList(); ZipInputStream zipInputStream = new ZipInputStream((Stream) File.OpenRead(zipPathAndFile)); if (password != null && password != string.Empty) zipInputStream.Password = password; string str = string.Empty; ZipEntry nextEntry; while ((nextEntry = zipInputStream.GetNextEntry()) != null) arrayList.Add((object) nextEntry); zipInputStream.Close(); return arrayList; }
public static void UnZipFiles(string zipPathAndFile, string outputFolder, string password, bool deleteZipFile) { ZipInputStream s = new ZipInputStream(File.OpenRead(zipPathAndFile)); if (password != null && password != String.Empty) s.Password = password; ZipEntry theEntry; string tmpEntry = String.Empty; while ((theEntry = s.GetNextEntry()) != null) { string directoryName = outputFolder; string fileName = Path.GetFileName(theEntry.Name); // create directory if (directoryName != "") { Directory.CreateDirectory(directoryName); } if (fileName != String.Empty) { if (theEntry.Name.IndexOf(".ini") < 0) { string fullPath = directoryName + "\\" + theEntry.Name; fullPath = fullPath.Replace("\\ ", "\\"); string fullDirPath = Path.GetDirectoryName(fullPath); if (!Directory.Exists(fullDirPath)) Directory.CreateDirectory(fullDirPath); FileStream streamWriter = File.Create(fullPath); 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(); if (deleteZipFile) { File.Delete(zipPathAndFile); } }
public static bool UnZip(string filename, string directory) { bool bSuccee = false; try { ZipEntry theEntry; int Cnt = directory.Length; if (directory.Substring(Cnt - 1) != @"\") { directory = directory + @"\"; } if (!Directory.Exists(directory)) { Directory.CreateDirectory(directory); } ZipInputStream s = new ZipInputStream(File.OpenRead(filename)); while ((theEntry = s.GetNextEntry( )) != null) { string directoryName = Path.GetDirectoryName(theEntry.Name); string fileName = Path.GetFileName(theEntry.Name); if (directoryName != string.Empty) { Directory.CreateDirectory(directory + directoryName); } if (fileName != string.Empty) { FileStream streamWriter = File.Create(Path.Combine(directory, (theEntry.Name.Substring(0, 1) == @"\") ? theEntry.Name.Substring(1) : theEntry.Name)); int size = 0x800; byte[] data = new byte[0x800]; while (true) { size = s.Read(data, 0, data.Length); if (size <= 0) { break; } streamWriter.Write(data, 0, size); } streamWriter.Close( ); } } s.Close( ); bSuccee = true; } catch (Exception) { throw; } return bSuccee; }
/// <summary> /// 将zip文件解压到一个目录 /// </summary> /// <param name="file">目标zip文件</param> /// <param name="directory">解压的目标文件夹</param> /// <returns></returns> public bool UnZipFiles(string file, string directory) { try { if (!File.Exists(file)) return false; directory = directory.Replace("/", "\\"); if (!directory.EndsWith("\\")) directory += "\\"; if (!Directory.Exists(directory)) Directory.CreateDirectory(directory); ZipInputStream s = new ZipInputStream(File.OpenRead(file)); ZipEntry theEntry; while ((theEntry = s.GetNextEntry()) != null) { string directoryName = Path.GetDirectoryName(theEntry.Name); string fileName = Path.GetFileName(theEntry.Name); if (directoryName != String.Empty) Directory.CreateDirectory(directory + directoryName); if (fileName != String.Empty) { FileStream streamWriter = File.Create(directory + 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(); return true; } catch (Exception) { return false; } }
public static void Main(string[] args) { // Perform simple parameter checking. if ( args.Length < 1 ) { Console.WriteLine("Usage ViewZipFile NameOfFile"); return; } if ( !File.Exists(args[0]) ) { Console.WriteLine("Cannot find file '{0}'", args[0]); return; } // For IO there should be exception handling but in this case its been ommitted byte[] data = new byte[4096]; using (ZipInputStream s = new ZipInputStream(File.OpenRead(args[0]))) { ZipEntry theEntry; while ((theEntry = s.GetNextEntry()) != null) { Console.WriteLine("Name : {0}", theEntry.Name); Console.WriteLine("Date : {0}", theEntry.DateTime); Console.WriteLine("Size : (-1, if the size information is in the footer)"); Console.WriteLine(" Uncompressed : {0}", theEntry.Size); Console.WriteLine(" Compressed : {0}", theEntry.CompressedSize); if ( theEntry.IsFile ) { // Assuming the contents are text may be ok depending on what you are doing // here its fine as its shows how data can be read from a Zip archive. Console.Write("Show entry text (y/n) ?"); if (Console.ReadLine() == "y") { int size = s.Read(data, 0, data.Length); while (size > 0) { Console.Write(Encoding.ASCII.GetString(data, 0, size)); size = s.Read(data, 0, data.Length); } } Console.WriteLine(); } } // Close can be ommitted as the using statement will do it automatically // but leaving it here reminds you that is should be done. s.Close(); } }
/// <summary> /// 解压文件到指定目录 /// </summary> /// <param name="inputFileName">输入文件</param> /// <param name="outputFileName">输出文件</param> /// <returns></returns> public bool UnZipJieya(string inputFileName, string outputFileName) { string directoryName = outputFileName; if (!Directory.Exists(directoryName)) Directory.CreateDirectory(directoryName);//生成解压目录 string CurrentDirectory = directoryName; byte[] data = new byte[2048]; int size = 2048; ZipEntry theEntry = null; using (ZipInputStream s = new ZipInputStream(File.OpenRead(inputFileName))) { while ((theEntry = s.GetNextEntry()) != null) { if (theEntry.IsDirectory) {// 该结点是目录 if (!Directory.Exists(CurrentDirectory + theEntry.Name)) Directory.CreateDirectory(CurrentDirectory + theEntry.Name); } else { if (theEntry.Name != String.Empty) { // 检查多级目录是否存在 if (theEntry.Name.Contains("//")) { string parentDirPath = theEntry.Name.Remove(theEntry.Name.LastIndexOf("//") + 1); if (!Directory.Exists(parentDirPath)) { Directory.CreateDirectory(CurrentDirectory + parentDirPath); } } //解压文件到指定的目录 using (FileStream streamWriter = File.Create(CurrentDirectory + theEntry.Name)) { while (true) { size = s.Read(data, 0, data.Length); if (size <= 0) break; streamWriter.Write(data, 0, size); } streamWriter.Close(); } } } } s.Close(); } return true; }
/// <summary> /// Unpack the file no progress. /// </summary> public void Unpack(string file, string dir) { try { if (!Directory.Exists(dir)) { Directory.CreateDirectory(dir); } ICSharpCode.SharpZipLib.Zip.ZipInputStream s = new ICSharpCode.SharpZipLib.Zip.ZipInputStream(File.OpenRead(file)); ICSharpCode.SharpZipLib.Zip.ZipEntry theEntry; byte[] data = new byte[2048]; while ((theEntry = s.GetNextEntry()) != null) { string directoryName = Path.GetDirectoryName(theEntry.Name); string fileName = Path.GetFileName(theEntry.Name); if (directoryName != String.Empty) { Directory.CreateDirectory(dir + directoryName); } if (fileName != String.Empty) { CurFileName = theEntry.Name; FileStream streamWriter = File.Create(dir + theEntry.Name); int size = 2048; while (true) { size = s.Read(data, 0, data.Length); if (size > 0) { streamWriter.Write(data, 0, size); } else { break; } } streamWriter.Close(); } } s.Close(); GC.Collect(); }catch (Exception) { throw; } }
public static void UnZipFiles(string zipPathAndFile, string outputFolder, bool deleteZipFile) { try { var s = new ZipInputStream(File.OpenRead(zipPathAndFile)); ZipEntry theEntry; while ((theEntry = s.GetNextEntry()) != null) { var directoryName = outputFolder; var fileName = Path.GetFileName(theEntry.Name); // create directory if (directoryName != "") { Directory.CreateDirectory(directoryName); } if (fileName == String.Empty || theEntry.Name.IndexOf(".ini") >= 0) continue; var fullPath = directoryName + "\\" + theEntry.Name; fullPath = fullPath.Replace("\\ ", "\\"); var fullDirPath = Path.GetDirectoryName(fullPath); if (fullDirPath != null && !Directory.Exists(fullDirPath)) Directory.CreateDirectory(fullDirPath); var streamWriter = File.Create(fullPath); var data = new byte[2048]; while (true) { var size = s.Read(data, 0, data.Length); if (size > 0) { streamWriter.Write(data, 0, size); } else { break; } } streamWriter.Close(); } s.Close(); if (deleteZipFile) File.Delete(zipPathAndFile); } catch (Exception e) { MessageBox.Show(e.Message); } }
//文件解压 public void UnZip(string zipedFile, string strDirectory, string password, bool overWrite) { if (strDirectory == "") strDirectory = Directory.GetCurrentDirectory(); if (!strDirectory.EndsWith("\\")) strDirectory = strDirectory + "\\"; using (ZipInputStream s = new ZipInputStream(File.OpenRead(zipedFile))) { s.Password = password; ZipEntry theEntry; while ((theEntry = s.GetNextEntry()) != null) { string directoryName = ""; string pathToZip = ""; pathToZip = theEntry.Name; if (pathToZip != "") directoryName = Path.GetDirectoryName(pathToZip) + "\\"; string fileName = Path.GetFileName(pathToZip); Directory.CreateDirectory(strDirectory + directoryName); if (fileName != "") { if ((File.Exists(strDirectory + directoryName + fileName) && overWrite) || (!File.Exists(strDirectory + directoryName + fileName))) { using (FileStream streamWriter = File.Create(strDirectory + directoryName + 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; } streamWriter.Close(); } } } } s.Close(); } }
public bool UnZip(string sourceFile, string destinationFile) { bool flag = true; try { ZipEntry entry; ZipInputStream stream = new ZipInputStream(File.OpenRead(sourceFile)); while ((entry = stream.GetNextEntry()) != null) { bool flag3; string directoryName = Path.GetDirectoryName(destinationFile); if (!(Path.GetFileName(entry.Name.Replace("updatepacket/", "")) != string.Empty)) { goto Label_010A; } if (entry.CompressedSize == 0L) { break; } Directory.CreateDirectory(Path.GetDirectoryName(destinationFile + entry.Name.Replace("updatepacket/", ""))); FileStream stream2 = File.Create(destinationFile + entry.Name.Replace("updatepacket/", "")); int count = 0x800; byte[] buffer = new byte[0x800]; goto Label_00FC; Label_00C9: count = stream.Read(buffer, 0, buffer.Length); if (count > 0) { stream2.Write(buffer, 0, count); } else { goto Label_0101; } Label_00FC: flag3 = true; goto Label_00C9; Label_0101: stream2.Close(); Label_010A:; } stream.Close(); } catch { flag = false; } return flag; }
/// <summary> /// Unpacks the files. /// </summary> /// <param name="file">The file.</param> /// <returns>if succeed return true,otherwise false.</returns> public static bool UnpackFiles(Stream stream, string dir) { try { if (!Directory.Exists(dir)) Directory.CreateDirectory(dir); ZipInputStream s = new ZipInputStream(stream); ZipEntry theEntry; while ((theEntry = s.GetNextEntry()) != null) { string directoryName = Path.GetDirectoryName(theEntry.Name); string fileName = Path.GetFileName(theEntry.Name); if (directoryName != String.Empty) Directory.CreateDirectory(dir + directoryName); if (fileName != String.Empty) { FileStream streamWriter = File.Create(dir + 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(); return true; } catch (Exception) { throw; } }
/// <summary> /// 压缩 /// </summary> /// <param name="param"></param> /// <returns></returns> static public string CompressStrZip(string param) { byte[] data = System.Text.Encoding.UTF8.GetBytes(param); //byte[] data = Convert.FromBase64String(param); MemoryStream ms = new MemoryStream(); Stream stream = new ICSharpCode.SharpZipLib.Zip.ZipInputStream(ms); try { stream.Write(data, 0, data.Length); } finally { stream.Close(); ms.Close(); } return(Convert.ToBase64String(ms.ToArray())); }
/// <summary> /// 解压 /// </summary> /// <param name="param"></param> /// <returns></returns> static public string DecompressStrZip(string param) { string commonString = ""; byte[] buffer = System.Text.Encoding.UTF8.GetBytes(param); //byte[] buffer=Convert.FromBase64String(param); MemoryStream ms = new MemoryStream(buffer); Stream sm = new ICSharpCode.SharpZipLib.Zip.ZipInputStream(ms); //这里要指明要读入的格式,要不就有乱码 StreamReader reader = new StreamReader(sm, System.Text.Encoding.UTF8); try { commonString = reader.ReadToEnd(); } finally { sm.Close(); ms.Close(); } return(commonString); }
/// <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); } }
IEnumerator UnpackAsync(string file, string dir, int count) { int index = 0; if (!Directory.Exists(dir)) { Directory.CreateDirectory(dir); } ICSharpCode.SharpZipLib.Zip.ZipInputStream s = new ICSharpCode.SharpZipLib.Zip.ZipInputStream(File.OpenRead(file)); ICSharpCode.SharpZipLib.Zip.ZipEntry theEntry; byte[] data = new byte[1024]; while ((theEntry = s.GetNextEntry()) != null) { index++; if (index % (count / 50) == 0) { yield return(0); } Progress = (float)index / (float)count; string directoryName = Path.GetDirectoryName(theEntry.Name); string fileName = Path.GetFileName(theEntry.Name); if (directoryName != String.Empty) { Directory.CreateDirectory(dir + directoryName); } if (fileName != String.Empty) { // FileStream streamWriter = File.Create(dir + theEntry.Name); // int size = 2048; // while (true) // { // size = s.Read(data, 0, data.Length); // if (size > 0) // { // streamWriter.Write(data, 0, size); // } // else // { // break; // } // } // streamWriter.Close(); CurFileName = theEntry.Name; using (FileStream streamWriter = File.Create(dir + theEntry.Name)) { int size = s.Read(data, 0, data.Length); while (size > 0) { streamWriter.Write(data, 0, size); size = s.Read(data, 0, data.Length); } streamWriter.Close(); } } } Progress = 1; s.Close(); GC.Collect(); }
// 해제 static public bool DeCompression(string zipPath, string dest, ref string Exception) { string extractDir = dest; System.IO.FileStream fs = new System.IO.FileStream(zipPath, System.IO.FileMode.Open, System.IO.FileAccess.Read, System.IO.FileShare.Read); ICSharpCode.SharpZipLib.Zip.ZipInputStream zis = new ICSharpCode.SharpZipLib.Zip.ZipInputStream(fs); bool bRes = true; try { long t_size = 0; ICSharpCode.SharpZipLib.Zip.ZipEntry ze; while ((ze = zis.GetNextEntry()) != null) { if (!ze.IsDirectory) { string fileName = System.IO.Path.GetFileName(ze.Name); string destDir = System.IO.Path.Combine(extractDir, System.IO.Path.GetDirectoryName(ze.Name)); if (false == Directory.Exists(destDir)) { System.IO.Directory.CreateDirectory(destDir); } string destPath = System.IO.Path.Combine(destDir, fileName); System.IO.FileStream writer = new System.IO.FileStream( destPath, System.IO.FileMode.Create, System.IO.FileAccess.Write, System.IO.FileShare.Write); byte[] buffer = new byte[2048]; int len; while ((len = zis.Read(buffer, 0, buffer.Length)) > 0) { writer.Write(buffer, 0, len); } writer.Close(); if (m_CB_DeCompress != null) { t_size += ze.Size; m_CB_DeCompress(t_size); } } } } catch (Exception e) { bRes = false; Exception = e.Message; } zis.Close(); fs.Close(); return(bRes); }