开发者

c# gzip - keep extension on original file but remove when compressing

开发者 https://www.devze.com 2023-03-02 01:03 出处:网络
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

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();
    }
}

}


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号