我正在尝试以编程方式解压缩压缩文件。

我尝试在.NET中使用System.IO.Compression.GZipStream类,但当我的应用程序运行时(实际上是一个单元测试),我得到这个异常:

System.IO.InvalidDataException: GZip头中的魔法数字不正确。确保你传递的是一个GZip流。

我现在意识到。Zip文件和。gz文件不一样,GZip和Zip也不一样。

然而,由于我能够通过手动双击压缩文件,然后单击“提取所有文件”按钮来提取文件,我认为在代码中也应该有一种方法来做到这一点。

因此,我尝试使用Process.Start(),并将压缩文件的路径作为输入。这导致我的应用程序打开一个窗口,显示压缩文件中的内容。这一切都很好,但应用程序将被安装在一个服务器上,没有人可以点击“提取所有文件”按钮。

那么,我如何让我的应用程序提取压缩文件中的文件?

或者还有别的办法吗?我更喜欢用代码来做,而不下载任何第三方库或应用程序;安全部门对此不太感兴趣……


当前回答

对于。net 4.5+

并不总是希望将未压缩的文件写入磁盘。作为ASP。Net开发人员,我将不得不摆弄权限来授予我的应用程序写入文件系统的权利。通过使用内存中的流,我可以绕过所有这些,直接读取文件:

using (ZipArchive archive = new ZipArchive(postedZipStream))
{
    foreach (ZipArchiveEntry entry in archive.Entries)
    {
         var stream = entry.Open();
         //Do awesome stream stuff!!
    }
}

或者,您仍然可以通过调用ExtractToFile()将解压缩的文件写入磁盘:

using (ZipArchive archive = ZipFile.OpenRead(pathToZip))
{
    foreach (ZipArchiveEntry entry in archive.Entries)
    {
        entry.ExtractToFile(Path.Combine(destination, entry.FullName));
    }
} 

要使用ZipArchive类,您需要添加对System.IO.Compression命名空间和System.IO.Compression. filesystem的引用。

其他回答

在http://www.codeplex.com/DotNetZip上使用DotNetZip库

class library and toolset for manipulating zip files. Use VB, C# or any .NET language to easily create, extract, or update zip files... DotNetZip works on PCs with the full .NET Framework, and also runs on mobile devices that use the .NET Compact Framework. Create and read zip files in VB, C#, or any .NET language, or any scripting environment... If all you want is a better DeflateStream or GZipStream class to replace the one that is built-into the .NET BCL, DotNetZip has that, too. DotNetZip's DeflateStream and GZipStream are available in a standalone assembly, based on a .NET port of Zlib. These streams support compression levels and deliver much better performance than the built-in classes. There is also a ZlibStream to complete the set (RFC 1950, 1951, 1952)...

标准zip文件通常使用deflate算法。

要在不使用第三方库的情况下提取文件,请使用deflestream。您需要更多关于zip文件归档格式的信息,因为Microsoft只提供压缩算法。

您也可以尝试使用zipfldr.dll。它是微软的压缩库(从“发送到”菜单压缩文件夹)。它看起来是一个com库,但它是无文档的。你可以通过实验让它为你工作。

这将完成system . io . compress . zipfile。ExtractToDirectory (ZipName ExtractToPath)

您可以使用Info-unzip命令行cod。你只需要从Info-unzip官方网站下载unzip.exe即可。

 internal static void Unzip(string sorcefile)
    {
        try
        {
            AFolderFiles.AFolderFilesDelete.DeleteFolder(TempBackupFolder); // delete old folder   
            AFolderFiles.AFolderFilesCreate.CreateIfNotExist(TempBackupFolder); // delete old folder   
           //need to Command command also to export attributes to a excel file
            System.Diagnostics.Process process = new System.Diagnostics.Process();
            System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
            startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden; // window type
            startInfo.FileName = UnzipExe;
            startInfo.Arguments = sorcefile + " -d " + TempBackupFolder;
            process.StartInfo = startInfo;
            process.Start();
            //string result = process.StandardOutput.ReadToEnd();
            process.WaitForExit();
            process.Dispose();
            process.Close();
        }
        catch (Exception ex){ throw ex; }
    }        

从这里开始:

已写入压缩的GZipStream对象 扩展名为。gz的文件可以 常用的解压方法有很多 压缩工具;然而,这个类 并不天生提供 向或中添加文件的功能 从.zip档案中提取文件。