GZipStream zip 文件无效或已损坏
我在打开 zip 文件时遇到问题。我正在使用此代码来压缩文件:
public static string Zip_File(string soruce , string target)
{
try
{
byte[] bufferWrite;
using (FileStream fsSource = new FileStream(soruce, FileMode.Open, FileAccess.Read, FileShare.Read))
{
bufferWrite = new byte[fsSource.Length];
fsSource.Read(bufferWrite, 0, bufferWrite.Length);
using (FileStream fsDest = new FileStream(target, FileMode.OpenOrCreate, FileAccess.Write))
{
using (GZipStream gzCompressed = new GZipStream(fsDest, CompressionMode.Compress, true))
{
gzCompressed.Write(bufferWrite, 0, bufferWrite.Length);
bufferWrite = null;
fsSource.Close();
gzCompressed.Close();
fsDest.Close();
}
}
}
return "success";
}
catch (Exception ex)
{
return ex.Message;
}
}
当我调用此函数时,我收到“成功”消息,但无法打开压缩文件。这是我的函数调用代码:
ZipFiles.Zip_File(@"C:\Documents and Settings\ccspl\Desktop\IntegrityDVR.mdb", @"C:\Documents and Settings\ccspl\Desktop\a.zip")
这是我收到的错误消息:
压缩(文件夹)无效或已损坏
I have a problem when opening a zip file. I am using this code to zip the file:
public static string Zip_File(string soruce , string target)
{
try
{
byte[] bufferWrite;
using (FileStream fsSource = new FileStream(soruce, FileMode.Open, FileAccess.Read, FileShare.Read))
{
bufferWrite = new byte[fsSource.Length];
fsSource.Read(bufferWrite, 0, bufferWrite.Length);
using (FileStream fsDest = new FileStream(target, FileMode.OpenOrCreate, FileAccess.Write))
{
using (GZipStream gzCompressed = new GZipStream(fsDest, CompressionMode.Compress, true))
{
gzCompressed.Write(bufferWrite, 0, bufferWrite.Length);
bufferWrite = null;
fsSource.Close();
gzCompressed.Close();
fsDest.Close();
}
}
}
return "success";
}
catch (Exception ex)
{
return ex.Message;
}
}
When I call this function, I am receiving "success" message, but I can't open the zip file. This is my function call code:
ZipFiles.Zip_File(@"C:\Documents and Settings\ccspl\Desktop\IntegrityDVR.mdb", @"C:\Documents and Settings\ccspl\Desktop\a.zip")
This is the error message I receive:
the compressed(folder) is invalid or corrupted
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
GZipStream
不会创建.zip
文件。它创建.gz
文件。如果您需要创建.zip
文件,您应该使用类似 SharpZipLib< /a>.GZipStream
does not create.zip
files. It creates.gz
files. If you need to create.zip
files, you should use something like SharpZipLib.但是,等一下,GZipStream 不会创建 zip 文件,它会创建 gzip 文件,据我所知, 使用 GZipStream 压缩文件 应该会有所帮助
but, wait a minute, GZipStream doesn't create zip file, it creates gzip files as I know, Zipping files using GZipStream should help
为什么不使用 SharpZipLib ?它使这变得容易得多。
Why not use SharpZipLib? It makes this a lot easier.
DotNetZip(开源 zip 库)的示例代码。
sample code for DotNetZip, an open source zip library.