GZip解压给出空白文件

发布于 2024-11-16 00:34:34 字数 1171 浏览 3 评论 0原文

给出下面的代码,为什么解压不起作用? “NewFile2.txt”应该包含原始的解压缩文本,但该文件只是空白。

ioTests.CompressFile(@"c:\newfile.txt", @"c:\newfile.txt.gz");
ioTests.DecompressFile(@"c:\newfile.txt.gz", @"c:\newfile2.txt");

public void CompressFile(string inFileName, string outFileName)
{
    FileStream inFile = new FileStream(inFileName, FileMode.Open);
    FileStream outFile = new FileStream(outFileName, FileMode.Create);

    GZipStream compStream = new GZipStream(outFile, CompressionMode.Compress);

    int theByte = inFile.ReadByte();

    while (theByte != -1)
    {
        compStream.WriteByte((byte)theByte);
        theByte = inFile.ReadByte();
    }

    compStream.Close();
}

public void DecompressFile(string inFileName, string outFileName)
{
    FileStream inFile = new FileStream(inFileName, FileMode.Open);
    FileStream outFile = new FileStream(outFileName, FileMode.CreateNew);

    GZipStream compStream = new GZipStream(inFile, CompressionMode.Decompress);

    int theByte = compStream.ReadByte();

    while (theByte != -1)
    {
        outFile.WriteByte((byte)theByte);

        theByte = compStream.ReadByte();
    }

    compStream.Close();
}

Given the code below, why is the decompression not working? "NewFile2.txt" should have the original, decompressed text, but the file is just blank.

ioTests.CompressFile(@"c:\newfile.txt", @"c:\newfile.txt.gz");
ioTests.DecompressFile(@"c:\newfile.txt.gz", @"c:\newfile2.txt");

public void CompressFile(string inFileName, string outFileName)
{
    FileStream inFile = new FileStream(inFileName, FileMode.Open);
    FileStream outFile = new FileStream(outFileName, FileMode.Create);

    GZipStream compStream = new GZipStream(outFile, CompressionMode.Compress);

    int theByte = inFile.ReadByte();

    while (theByte != -1)
    {
        compStream.WriteByte((byte)theByte);
        theByte = inFile.ReadByte();
    }

    compStream.Close();
}

public void DecompressFile(string inFileName, string outFileName)
{
    FileStream inFile = new FileStream(inFileName, FileMode.Open);
    FileStream outFile = new FileStream(outFileName, FileMode.CreateNew);

    GZipStream compStream = new GZipStream(inFile, CompressionMode.Decompress);

    int theByte = compStream.ReadByte();

    while (theByte != -1)
    {
        outFile.WriteByte((byte)theByte);

        theByte = compStream.ReadByte();
    }

    compStream.Close();
}

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

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

发布评论

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

评论(3

千里故人稀 2024-11-23 00:34:34
outFile.Flush(); // after your loop
outFile.Flush(); // after your loop
时间海 2024-11-23 00:34:34

我更喜欢

outFile.Close() 

这样刷新流并调用 Dispose 方法,释放分配的资源。

I prefer

outFile.Close() 

as that flushes the stream and calls the Dispose method, releasing allocated resources.

动次打次papapa 2024-11-23 00:34:34

由于您使用的流实现了 IDisposable 接口< /a>,您应该 Dispose() / Close() 您的类,或使用 using 语句自动执行此操作:

    using (FileStream inFile = new FileStream(inFileName, FileMode.Open))
    using (FileStream outFile = new FileStream(outFileName, FileMode.Create))

    using (GZipStream compStream = new GZipStream(outFile, CompressionMode.Compress)) {

        int theByte = inFile.ReadByte();
        // ... Rest of your code

    }

这大致翻译为到:

    try {
       FileStream inFile = new FileStream(inFileName, FileMode.Open);
       FileStream outFile = new FileStream(outFileName, FileMode.Create);
       GZipStream compStream = new GZipStream(outFile, CompressionMode.Compress);

        int theByte = inFile.ReadByte();
        // ... Rest of your code

    } finally {
        if (inFile != null) inFile.Dispose();
        if (outFile != null) outFile.Dispose();
        if (compStream != null) compStream.Dispose();
    }

Since the streams you use implement the IDisposable interface, you should Dispose() / Close() your classes, or use the using statement to do this automatically:

    using (FileStream inFile = new FileStream(inFileName, FileMode.Open))
    using (FileStream outFile = new FileStream(outFileName, FileMode.Create))

    using (GZipStream compStream = new GZipStream(outFile, CompressionMode.Compress)) {

        int theByte = inFile.ReadByte();
        // ... Rest of your code

    }

This roughly translates to:

    try {
       FileStream inFile = new FileStream(inFileName, FileMode.Open);
       FileStream outFile = new FileStream(outFileName, FileMode.Create);
       GZipStream compStream = new GZipStream(outFile, CompressionMode.Compress);

        int theByte = inFile.ReadByte();
        // ... Rest of your code

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