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

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

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

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

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

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

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

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


当前回答

从这里开始:

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

其他回答

您可以使用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; }
    }        

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

我用它来压缩或解压缩多个文件。Regex的东西不是必需的,但我用它来更改日期戳和删除不需要的下划线。如果需要,我使用压缩>> zipPath字符串中的空字符串为所有文件添加前缀。此外,我通常根据我正在做的事情注释掉Compress()或Decompress()。

using System;
using System.IO.Compression;
using System.IO;
using System.Text.RegularExpressions;

namespace ZipAndUnzip
{
    class Program
    {
        static void Main(string[] args)
        {
            var directoryPath = new DirectoryInfo(@"C:\your_path\");

            Compress(directoryPath);
            Decompress(directoryPath);
        }

        public static void Compress(DirectoryInfo directoryPath)
        {
            foreach (DirectoryInfo directory in directoryPath.GetDirectories())
            {
                var path = directoryPath.FullName;
                var newArchiveName = Regex.Replace(directory.Name, "[0-9]{8}", "20130913");
                newArchiveName = Regex.Replace(newArchiveName, "[_]+", "_");
                string startPath = path + directory.Name;
                string zipPath = path + "" + newArchiveName + ".zip";

                ZipFile.CreateFromDirectory(startPath, zipPath);
            }

        }

        public static void Decompress(DirectoryInfo directoryPath)
        {
            foreach (FileInfo file in directoryPath.GetFiles())
            {
                var path = directoryPath.FullName;
                string zipPath = path + file.Name;
                string extractPath = Regex.Replace(path + file.Name, ".zip", "");

                ZipFile.ExtractToDirectory(zipPath, extractPath);
            }
        }


    }
}

从这里开始:

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

你可以在. net 3.5中使用DeflateStream来完成这一切。. net 3.5中缺少的是处理用于组织压缩文件的文件头部分的能力。PKWare已经发布了这些信息,您可以在创建使用的结构后使用这些信息来处理zip文件。它不是特别繁重,而且在不使用第三方代码的情况下构建工具是一种很好的实践。

这不是一个简单的答案,但如果你愿意并且能够自己花时间,这是完全可行的。我在几个小时内编写了一个类来完成这个任务,从中我得到的是仅使用。net 3.5压缩和解压缩文件的能力。