ICSharpCode.SharpZipLib.Zip 下如何对一个byte[] 压缩,解压?

生财 2010-08-16 09:38:44
ICSharpCode.SharpZipLib.Zip 下如何对一个byte[] 压缩,解压?
我在网上找到的都是针对文件的。
有没有对buffer 进行压缩,解压?的示例或者代码呀
...全文
219 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
生财 2010-08-16
  • 打赏
  • 举报
回复
结账。。。。。。。。。
smilef9453 2010-08-16
  • 打赏
  • 举报
回复
ZipInputStream stream = new ZipInputStream(File.OpenRead(ZipFilePath))
其实ZipInputStream读取的是流,只要把byte[] 读到流中就可以了


Stream 和 byte[] 之间的转换

/* - - - - - - - - - - - - - - - - - - - - - - - -
* Stream 和 byte[] 之间的转换
* - - - - - - - - - - - - - - - - - - - - - - - */
/// <summary>
/// 将 Stream 转成 byte[]
/// </summary>
public byte[] StreamToBytes(Stream stream)
{
byte[] bytes = new byte[stream.Length];
stream.Read(bytes, 0, bytes.Length);
// 设置当前流的位置为流的开始
stream.Seek(0, SeekOrigin.Begin);
return bytes;
}
/// <summary>
/// 将 byte[] 转成 Stream
/// </summary>
public Stream BytesToStream(byte[] bytes)
{
Stream stream = new MemoryStream(bytes);
return stream;
}
/* - - - - - - - - - - - - - - - - - - - - - - - -
* Stream 和 文件之间的转换
* - - - - - - - - - - - - - - - - - - - - - - - */
/// <summary>
/// 将 Stream 写入文件
/// </summary>
public void StreamToFile(Stream stream,string fileName)
{
// 把 Stream 转换成 byte[]
byte[] bytes = new byte[stream.Length];
stream.Read(bytes, 0, bytes.Length);
// 设置当前流的位置为流的开始
stream.Seek(0, SeekOrigin.Begin);
// 把 byte[] 写入文件
FileStream fs = new FileStream(fileName, FileMode.Create);
BinaryWriter bw = new BinaryWriter(fs);
bw.Write(bytes);
bw.Close();
fs.Close();
}
/// <summary>
/// 从文件读取 Stream
/// </summary>
public Stream FileToStream(string fileName)
{
// 打开文件
FileStream fileStream = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.Read);
// 读取文件的 byte[]
byte[] bytes = new byte[fileStream.Length];
fileStream.Read(bytes, 0, bytes.Length);
fileStream.Close();
// 把 byte[] 转换成 Stream
Stream stream = new MemoryStream(bytes);
return stream;
}

zip压缩DLL 可以使用程序自己来控制 压缩,解压. c#使用代码如下: /// /// 压缩解压文件 /// public class ZipClass { /// /// 所有文件缓存 /// List files = new List(); /// /// 所有空目录缓存 /// List paths = new List(); /// /// 压缩单个文件 /// /// 要压缩的文件 /// 压缩后的文件全名 /// 压缩程度,范围0-9,数值越大,压缩程序越高 /// 分块大小 public void ZipFile(string fileToZip, string zipedFile, int compressionLevel, int blockSize) { if (!System.IO.File.Exists(fileToZip))//如果文件没有找到,则报错 { throw new FileNotFoundException("The specified file " + fileToZip + " could not be found. Zipping aborderd"); } FileStream streamToZip = new FileStream(fileToZip, FileMode.Open, FileAccess.Read); FileStream zipFile = File.Create(zipedFile); ZipOutputStream zipStream = new ZipOutputStream(zipFile); ZipEntry zipEntry = new ZipEntry(fileToZip); zipStream.PutNextEntry(zipEntry); zipStream.SetLevel(compressionLevel); byte[] buffer = new byte[blockSize]; int size = streamToZip.Read(buffer, 0, buffer.Length); zipStream.Write(buffer, 0, size); try { while (size < streamToZip.Length) { int sizeRead = streamToZip.Read(buffer, 0, buffer.Length); zipStream.Write(buffer, 0, sizeRead); size += sizeRead; } } catch (Exception ex) { GC.Collect(); throw ex; } zipStream.Finish(); zipStream.Close(); streamToZip.Close(); GC.Collect(); } /// /// 压缩目录(包括子目录及所有文件) /// /// 要压缩的根目录 /// 保存路径 /// 压缩程度,范围0-9,数值越大,压缩程序越高 public void ZipFileFromDirectory(string rootPath, string destinationPath, int compressLevel) { GetAllDirectories(rootPath); /* while (rootPath.LastIndexOf("\\") + 1 == rootPath.Length)//检查路径是否以"\"结尾 { rootPath = rootPath.Substring(0, rootPath.Length - 1);//如果是则去掉末尾的"\" } */ //string rootMark = rootPath.Substring(0, rootPath.LastIndexOf("\\") + 1);//得到当前路径的位置,以备压缩时将所压缩内容转变成相对路径。 string rootMark = rootPath + "\\";//得到当前路径的位置,以备压缩时将所压缩内容转变成相对路径。 Crc32 crc = new Crc32(); ZipOutputStream outPutStream = new ZipOutputStream(File.Create(destinationPath)); outPutStream.SetLevel(compressLevel); // 0 - store only to 9 - means best compression foreach (string file in files) { FileStream fileStream = File.OpenRead(file);//打开压缩文件 byte[] buffer = new byte[fileStream.Length]; fileStream.Read(buffer, 0, buffer.Length); ZipEntry entry = new ZipEntry(file.Replace(rootMark, string.Empty)); entry.DateTime = DateTime.Now; // set Size and the crc, because the information // about the size and crc should be stored in the header // if it is not set it is automatically written in the footer. // (in this case size == crc == -1 in the header) // Some ZIP programs have problems with zip files that don't store // the size and crc in the header. entry.Size = fileStream.Length; fileStream.Close(); crc.Reset(); crc.Update(buffer); entry.Crc = crc.Value; outPutStream.PutNextEntry(entry); outPutStream.Write(buffer, 0, buffer.Length); } this.files.Clear(); foreach (string emptyPath in paths) { ZipEntry entry = new ZipEntry(emptyPath.Replace(rootMark, string.Empty) + "/"); outPutStream.PutNextEntry(entry); } this.paths.Clear(); outPutStream.Finish(); outPutStream.Close(); GC.Collect(); } /// /// 取得目录下所有文件及文件夹,分别存入files及paths /// /// 根目录 private void GetAllDirectories(string rootPath) { string[] subPaths = Directory.GetDirectories(rootPath);//得到所有子目录 foreach (string path in subPaths) { GetAllDirectories(path);//对每一个字目录做与根目录相同的操作:即找到子目录并将当前目录的文件名存入List } string[] files = Directory.GetFiles(rootPath); foreach (string file in files) { this.files.Add(file);//将当前目录中的所有文件全名存入文件List } if (subPaths.Length == files.Length && files.Length == 0)//如果是空目录 { this.paths.Add(rootPath);//记录空目录 } } /// /// 解压缩文件(压缩文件中含有子目录) /// /// 待解压缩的文件路径 /// 解压缩到指定目录 /// 解压后的文件列表 public List UnZip(string zipfilepath, string unzippath) { //解压出来的文件列表 List unzipFiles = new List(); //检查输出目录是否以“\\”结尾 if (unzippath.EndsWith("\\") == false || unzippath.EndsWith(":\\") == false) { unzippath += "\\"; } ZipInputStream s = new ZipInputStream(File.OpenRead(zipfilepath)); ZipEntry theEntry; while ((theEntry = s.GetNextEntry()) != null) { string directoryName = Path.GetDirectoryName(unzippath); string fileName = Path.GetFileName(theEntry.Name); //生成解压目录【用户解压到硬盘根目录时,不需要创建】 if (!string.IsNullOrEmpty(directoryName)) { Directory.CreateDirectory(directoryName); } if (fileName != String.Empty) { //如果文件的压缩后大小为0那么说明这个文件是空的,因此不需要进行读出写入 if (theEntry.CompressedSize == 0) break; //解压文件到指定的目录 directoryName = Path.GetDirectoryName(unzippath + theEntry.Name); //建立下面的目录和子目录 Directory.CreateDirectory(directoryName); //记录导出的文件 unzipFiles.Add(unzippath + theEntry.Name); FileStream streamWriter = File.Create(unzippath + 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(); GC.Collect(); return unzipFiles; } }
zip压缩相关 1using System; 2using System.Collections.Generic; 3using System.Text; 4using System.IO; 5using ICSharpCode.SharpZipLib; 6using ICSharpCode.SharpZipLib.Zip; 7using ICSharpCode.SharpZipLib.Checksums; 8 9/* 10 * 解压缩 11 * 该程序压缩解压配合才能使用 12 * 普通用 Winrar 压缩的文件该解压不能通过 13 * Modify By HJ 2007-10-25 14 */ 15namespace ZipApplication 16{ 17 /// 18 /// 压缩类 19 /// 20 public class ZipClass 21 { 22 /// 23 /// 递归压缩文件夹方法 24 /// 25 /// 26 /// 27 /// 28 private static bool ZipFileDictory(string FolderToZip, ZipOutputStream s, string ParentFolderName) 29 { 30 bool res = true; 31 string[] folders, filenames; 32 ZipEntry entry = null; 33 FileStream fs = null; 34 Crc32 crc = new Crc32(); 35 36 try 37 { 38 39 //创建当前文件夹 40 entry = new ZipEntry(Path.Combine(ParentFolderName, Path.GetFileName(FolderToZip) + "/")); //加上 “/” 才会当成是文件夹创建 41 s.PutNextEntry(entry); 42 s.Flush(); 43 44 45 //先压缩文件,再递归压缩文件夹 46 filenames = Directory.GetFiles(FolderToZip); 47 foreach (string file in filenames) 48 { 49 //打开压缩文件 50 fs = File.OpenRead(file); 51 52 byte[] buffer = new byte[fs.Length]; 53 fs.Read(buffer, 0, buffer.Length); 54 entry = new ZipEntry(Path.Combine(ParentFolderName, Path.GetFileName(FolderToZip) + "/" + Path.GetFileName(file))); 55 56 entry.DateTime = DateTime.Now; 57 entry.Size = fs.Length; 58 fs.Close(); 59 60 crc.Reset(); 61 crc.Update(buffer); 62 63 entry.Crc = crc.Value; 64 65 s.PutNextEntry(entry); 66 67 s.Write(buffer, 0, buffer.Length); 68 } 69 } 70 catch 71 { 72 res = false; 73 } 74 finally 75 { 76 if (fs != null) 77 { 78 fs.Close(); 79 fs = null; 80 } 81 if (entry != null) 82 { 83 entry = null; 84 } 85 GC.Collect(); 86 GC.Collect(1); 87 } 88 89 90 folders = Directory.GetDirectories(FolderToZip); 91 foreach (string folder in folders) 92 { 93 if (!ZipFileDictory(folder, s, Path.Combine(ParentFolderName, Path.GetFileName(FolderToZip)))) 94 { 95 return false; 96 } 97 } 98 99 return res; 100 } 101 102 /// 103 /// 压缩目录 104 /// 105 /// 压缩的文件夹,全路径格式 106 /// 压缩后的文件名,全路径格式 107 /// 108 private static bool ZipFileDictory(string FolderToZip, string ZipedFile, String Password) 109 { 110 bool res; 111 if (!Directory.Exists(FolderToZip)) 112 { 113 return false; 114 } 115 116 ZipOutputStream s = new ZipOutputStream(File.Create(ZipedFile)); 117 s.SetLevel(6); 118 s.Password = Password; 119 120 res = ZipFileDictory(FolderToZip, s, ""); 121 122 s.Finish(); 123 s.Close(); 124 125 return res; 126 } 127 128 /// 129 /// 压缩文件 130 /// 131 /// 要进行压缩的文件名 132 /// 压缩后生成的压缩文件名 133 /// 134 private static bool ZipFile(string FileToZip, string ZipedFile, String Password) 135 { 136 //如果文件没有找到,则报错 137 if (!File.Exists(FileToZip)) 138 { 139 throw new System.IO.FileNotFoundException("指定要压缩的文件: " + FileToZip + " 不存在!"); 140 } 141 //FileStream fs = null; 142 FileStream ZipFile = null; 143 ZipOutputStream ZipStream = null; 144 ZipEntry ZipEntry = null; 145 146 bool res = true; 147 try 148 { 149 ZipFile = File.OpenRead(FileToZip); 150 byte[] buffer = new byte[ZipFile.Length]; 151 ZipFile.Read(buffer, 0, buffer.Length); 152 ZipFile.Close(); 153 154 ZipFile = File.Create(ZipedFile); 155 ZipStream = new ZipOutputStream(ZipFile); 156 ZipStream.Password = Password; 157 ZipEntry = new ZipEntry(Path.GetFileName(FileToZip)); 158 ZipStream.PutNextEntry(ZipEntry); 159 ZipStream.SetLevel(6); 160 161 ZipStream.Write(buffer, 0, buffer.Length); 162 } 163 catch 164 { 165 res = false; 166 } 167 finally 168 { 169 if (ZipEntry != null) 170 { 171 ZipEntry = null; 172 } 173 if (ZipStream != null) 174 { 175 ZipStream.Finish(); 176 ZipStream.Close(); 177 } 178 if (ZipFile != null) 179 { 180 ZipFile.Close(); 181 ZipFile = null; 182 } 183 GC.Collect(); 184 GC.Collect(1); 185 } 186 187 return res; 188 } 189 190 /// 191 /// 压缩文件 和 文件夹 192 /// 193 /// 压缩的文件或文件夹,全路径格式 194 /// 压缩后生成的压缩文件名,全路径格式 195 /// 196 public static bool Zip(String FileToZip, String ZipedFile, String Password) 197 { 198 if (Directory.Exists(FileToZip)) 199 { 200 return ZipFileDictory(FileToZip, ZipedFile, Password); 201 } 202 else if (File.Exists(FileToZip)) 203 { 204 return ZipFile(FileToZip, ZipedFile, Password); 205 } 206 else 207 { 208 return false; 209 } 210 } 211 } 212 /// /// 解压类 /// public class UnZipClass { /// /// 解压功能(解压压缩文件到指定目录) /// /// 解压的文件 /// 指定解压目标目录 public static void UnZip(string FileToUpZip, string ZipedFolder,string Password) { if (!File.Exists(FileToUpZip)) { return; } if (!Directory.Exists(ZipedFolder)) { Directory.CreateDirectory(ZipedFolder); } ZipInputStream s = null; ZipEntry theEntry = null; string fileName; FileStream streamWriter = null; try { s = new ZipInputStream(File.OpenRead(FileToUpZip)); s.Password = Password; 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); } } } }

110,567

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧