此解压缩方法给我一个错误,其中该行(numRead = Decompress.Read(buffer, 0, buffer.Length)) != 0
给我一个错误,例如GZip
标头中的幻数不正确。确保您正在传递GZip
流。
我在这里用
发布了我的代码public bool Decompress(string zipPathAndFile, string outputFolder, string password, bool deleteZipFile)
{
FileInfo fi = new FileInfo(zipPathAndFile);
try
{
// Get the stream of the source file.
using (FileStream inFile = fi.OpenRead())
{
// Get original file extension, for example "doc" from report.doc.gz.
string curFile = fi.Name;
string origName = curFile.Remove(curFile.Length - fi.Extension.Length);
//Create the decompressed file.
using (FileStream outFile = File.Create(outputFolder.TrimEnd('\\') + '\\' + origName))
{
using (GZipStream Decompress = new GZipStream(inFile, CompressionMode.Decompress))
{
//Copy the decompression stream into the output file.
byte[] buffer = new byte[4096];
int numRead;
while ((numRead = Decompress.Read(buffer, 0, buffer.Length)) != 0)
{
outFile.Write(buffer, 0, numRead);
}
}
}
}
return true;
}
catch (System.Exception exc)
{
ExceptionManagerBll.ErrHandler(exc, "ERR", 3, new StackTrace(new StackFrame(true)).GetFrame(0).GetFileLineNumber(), new StackTrace(new StackFrame(true)).GetFrame(0).GetMethod().DeclaringType.Name, new StackTrace(new StackFrame(true)).GetFrame(0).GetMethod().Name, "Error is Occured", true, false, "", "");
return false;
}
}