使用C#解压缩.gz文件

27

如何使用c#解压.gz文件并将文件保存在指定文件夹中?

这是我第一次遇到.gz文件。我已经搜索了如何解压它,但对我来说没有用。它没有在指定文件夹中解压.gz文件。我不想使用任何第三方应用程序。

有人能给我一个示例代码,告诉我如何解压它并将文件保存在文件夹中吗?谢谢。


1
文档中包含一个示例:http://msdn.microsoft.com/en-us/library/system.io.compression.gzipstream.aspx - nos
它解压了文件,但却没有包含文件扩展名。 - Kuriyama Mirai
我的 .gz 文件包含 Excel 文件。但是当我解压缩它时,它只返回没有扩展名的文件。 - Kuriyama Mirai
1
@Kuriyama Mirai,你明白了吗? - Moez Rebai
可能是使用C#解压缩.gz文件的重复问题。 - psubsee2003
显示剩余4条评论
3个回答

53

以下示例来自MSDN,展示如何使用GZipStream类压缩和解压文件目录。

namespace zip
{
    public class Program
    {
        public static void Main()
        {
            string directoryPath = @"c:\users\public\reports";

            DirectoryInfo directorySelected = new DirectoryInfo(directoryPath);

            foreach (FileInfo fileToCompress in directorySelected.GetFiles())
            {
                Compress(fileToCompress);
            }

            foreach (FileInfo fileToDecompress in directorySelected.GetFiles("*.gz"))
            {
                Decompress(fileToDecompress);
            }
        }

        public static void Compress(FileInfo fileToCompress)
        {
            using (FileStream originalFileStream = fileToCompress.OpenRead())
            {
                if ((File.GetAttributes(fileToCompress.FullName) & FileAttributes.Hidden) != FileAttributes.Hidden & fileToCompress.Extension != ".gz")
                {
                    using (FileStream compressedFileStream = File.Create(fileToCompress.FullName + ".gz"))
                    {
                        using (GZipStream compressionStream = new GZipStream(compressedFileStream, CompressionMode.Compress))
                        {
                            originalFileStream.CopyTo(compressionStream);
                            Console.WriteLine("Compressed {0} from {1} to {2} bytes.",
                                fileToCompress.Name, fileToCompress.Length.ToString(), compressedFileStream.Length.ToString());
                        }
                    }
                }
            }
        }

        public static void Decompress(FileInfo fileToDecompress)
        {
            using (FileStream originalFileStream = fileToDecompress.OpenRead())
            {
                string currentFileName = fileToDecompress.FullName;
                string newFileName = currentFileName.Remove(currentFileName.Length - fileToDecompress.Extension.Length);

                using (FileStream decompressedFileStream = File.Create(newFileName))
                {
                    using (GZipStream decompressionStream = new GZipStream(originalFileStream, CompressionMode.Decompress))
                    {
                        decompressionStream.CopyTo(decompressedFileStream);
                        Console.WriteLine("Decompressed: {0}", fileToDecompress.Name);
                    }
                }
            }
        }
    }
}

我之前用过这段代码,但是它对我没起作用。我不知道为什么,它没有解压缩我的gz文件。它只是删除了gz扩展名,返回了没有扩展名的文件。 - Kuriyama Mirai
1
GZip只压缩一个文件,而且不知道它的名称。因此,如果您要压缩myReport.xls文件,则应将其命名为myReport.xls.gz。在解压缩时,最后一个文件扩展名将被删除,因此您将得到原始文件名。这是Unix/Linux使用的方式已经有很长时间了... - Moez Rebai
@KuriyamaMirai 看一下设置newFileName的那一行。 - Steven Liekens
我猜当您在文件名末尾添加.gz扩展名时,它会起作用,您试过了吗? - Moez Rebai
现在我知道了,我会尝试一下。非常感谢你。这回答了我的问题。 - Kuriyama Mirai

12

.Net拥有GZipStream

API中提供的示例...

public static void Decompress(FileInfo fileToDecompress)
    {
        using (FileStream originalFileStream = fileToDecompress.OpenRead())
        {
            string currentFileName = fileToDecompress.FullName;
            string newFileName = currentFileName.Remove(currentFileName.Length - fileToDecompress.Extension.Length);

            using (FileStream decompressedFileStream = File.Create(newFileName))
            {
                using (GZipStream decompressionStream = new GZipStream(originalFileStream, CompressionMode.Decompress))
                {
                    decompressionStream.CopyTo(decompressedFileStream);
                    Console.WriteLine("Decompressed: {0}", fileToDecompress.Name);
                }
            }
        }
    }

1
我之前用过这段代码,但是它对我没起作用。我不知道为什么,但它没有解压我的gz文件。它只是移除了gz扩展名,返回了没有扩展名的文件。 - Kuriyama Mirai

-2
以下链接展示了在C#中压缩和解压文件的两个示例。您可以使用这个样本。
示例(使用7-zip):
var tmp = new SevenZipCompressor();
tmp.ScanOnlyWritable = true;
tmp.CompressFilesEncrypted(outputFilePath, password, filePaths);

示例(使用ZipArchive):

ZipArchive zip = ZipFile.Open(filePath, ZipArchiveMode.Create);
zip.CreateEntryFromFile(file, Path.GetFileName(file), CompressionLevel.Optimal);
zip.Dispose();

更多信息:

http://csharpexamples.com/zip-and-unzip-files-programmatically-in-c/


这与.gz文件有什么关系? - NetMage

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接