关于ZipOupputStream添加压缩包常见问题
?
其實園子壓縮解壓縮的方法很多,ZipOupputStream這個類的說明很多,我這邊也是從網上找的代碼,但是我在壓縮的時候遇到了常見的兩個問題,第一個就是壓縮的時候讀取壓縮包報該壓縮包已經在另一個進程打開的bug,這個問題解決方法是不能把創建的壓縮包和被壓縮的文件放在同一目錄下,因為當前目錄已經被打開了。
第二個問題壓縮文件夾無法把多個文件壓縮,遍歷無法壓縮多個文件,原因是ZipOutputStream這個類庫中的CRC32這個類,本人表示查來查去也沒找個那個大神的園子里解釋過這個類,總之把與這個類相關的代碼注釋掉就可以多個文件壓縮了,希望有懂得大神可以給解釋下。
public class ZipFloClass
{
public void ZipFile(string strFile, string strZip)
{
if(File.Exists(strZip))
{
File.Delete(strZip);
}
if (strFile[strFile.Length - 1] != Path.DirectorySeparatorChar)
strFile += Path.DirectorySeparatorChar;
FileStream fs = new FileStream(strZip, FileMode.Create);
ZipOutputStream s = new ZipOutputStream(fs);
s.SetLevel(6); // 0 - store only to 9 - means best compression
zip(strFile, s, strFile);
s.Finish();
s.Close();
}
private void zip(string strFile, ZipOutputStream s, string staticFile)
{
if (strFile[strFile.Length - 1] != Path.DirectorySeparatorChar) strFile += Path.DirectorySeparatorChar;
Crc32 crc = new Crc32();
string[] filenames = Directory.GetFileSystemEntries(strFile);
foreach (string file in filenames)
{
if (Directory.Exists(file))
{
zip(file, s, staticFile);
}
else // 否則直接壓縮文件
{
//打開壓縮文件
FileStream fs = File.OpenRead(file);
byte[] buffer = new byte[fs.Length];
fs.Read(buffer, 0, buffer.Length);
string tempfile = file.Substring(staticFile.LastIndexOf("\\") + 1);
ZipEntry entry = new ZipEntry(tempfile);
entry.DateTime = DateTime.Now;
entry.Size = fs.Length;
fs.Close();
//crc.Reset();
//crc.Update(buffer);
//entry.Crc = crc.Value;
s.PutNextEntry(entry);
s.Write(buffer, 0, buffer.Length);
}
}
}
}
轉載于:https://www.cnblogs.com/renzhitian/p/7903119.html
總結
以上是生活随笔為你收集整理的关于ZipOupputStream添加压缩包常见问题的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Protobuf使用规范分享
- 下一篇: 静态方法和实例方法(mark)