c# gzip - 保留原始文件的扩展名,但在压缩时删除

发布于 2024-11-04 08:24:58 字数 1277 浏览 0 评论 0原文

基本上我正在尝试将文件“sample.doc”压缩为 .gz 文件格式。发生这种情况时,系统会告诉它删除文件的扩展名,而不是显示为 “sample.doc.gz”它显示为“sample.gz”。但是,当文件被提取时,它也丢失了“.doc”文件扩展名。例如。文件名只是“样本”。有什么想法吗?

使用系统; 使用系统.IO; 使用系统.IO.压缩; 使用系统文本;

名称空间 gzip 示例 {

class Program
{
    public static void Main()
    {
        CompressFile(@"C:\sample.doc");
   }


    //Compresses the file into a .gz file
    public static void CompressFile(string path)
    {
        string compressedPath = path;
        Console.WriteLine("Compressing: " + path);

        int extIndex = compressedPath.LastIndexOf(".");
        FileStream sourceFile = File.OpenRead(path);
        FileStream destinationFile = File.Create(compressedPath.Replace(compressedPath.Substring(extIndex), "") + ".gz");

        byte[] buffer = new byte[sourceFile.Length];

        sourceFile.Read(buffer, 0, buffer.Length);

        using (GZipStream output = new GZipStream(destinationFile,
            CompressionMode.Compress))
        {
            Console.WriteLine("Compressing {0} to {1}.", sourceFile.Name,
                destinationFile.Name, false);

            output.Write(buffer, 0, buffer.Length);
        }

        // Close the files.
        sourceFile.Close();
        destinationFile.Close();
    }
}

}

Basically I'm trying to compress a file "sample.doc" into the .gz file format. When this happens, it is told to remove the extension of the file so instead of appearing as
"sample.doc.gz" it appears as "sample.gz". However, when the file is extracted it has also lost its ".doc" file extension. eg. filename is just "sample". Any ideas?

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

namespace gzipexample
{

class Program
{
    public static void Main()
    {
        CompressFile(@"C:\sample.doc");
   }


    //Compresses the file into a .gz file
    public static void CompressFile(string path)
    {
        string compressedPath = path;
        Console.WriteLine("Compressing: " + path);

        int extIndex = compressedPath.LastIndexOf(".");
        FileStream sourceFile = File.OpenRead(path);
        FileStream destinationFile = File.Create(compressedPath.Replace(compressedPath.Substring(extIndex), "") + ".gz");

        byte[] buffer = new byte[sourceFile.Length];

        sourceFile.Read(buffer, 0, buffer.Length);

        using (GZipStream output = new GZipStream(destinationFile,
            CompressionMode.Compress))
        {
            Console.WriteLine("Compressing {0} to {1}.", sourceFile.Name,
                destinationFile.Name, false);

            output.Write(buffer, 0, buffer.Length);
        }

        // Close the files.
        sourceFile.Close();
        destinationFile.Close();
    }
}

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦里梦着梦中梦 2024-11-11 08:24:58

如果我正确理解你的问题,那么没有所述的解决方案。经过 gzip 压缩的文件(至少是按照您的方式进行 gzip 压缩的文件)不会存储其名称,因此,如果您压缩名为 example.doc 的文件并且输出名为 example.gz,则“ .doc”部分永远消失了。这就是为什么如果您使用 gzip 命令行实用程序压缩文件,它的压缩版本是sample.doc.gz。

在某些受限情况下,您可能可以通过查看文件的内容来猜测适当的扩展名,但这不太可靠。如果您只需要压缩,并且文件格式不受限制,您可以只构建一个 .zip 文件,该文件确实存储文件名。

If I'm understanding your question correctly, there is no solution as stated. A gzip'ed file (at least, a file gzip'ed the way you're doing it) doesn't store its name, so if you compress a file named sample.doc and the output is named sample.gz, the ".doc" part is gone forever. That's why if you compress a file with the gzip command-line utility, it the compressed version sample.doc.gz.

In some constrained situations, you might be able to guess an appropriate extension by looking at the contents of the file, but that isn't very reliable. If you just need compression, and the file format isn't constrained, you could just build a .zip file instead, which does store filenames.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文