How to compress and decompress file

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

Here are sample functions to compress and decompress a file:

private void TestCompress()
{
    string srcFile = "C://temp//file-to-compress.txt";
    string dstFile = "C://temp//compressed-file.gzip";

    FileStream fsIn = null; // will open and read the srcFile
    FileStream fsOut = null; // will be used by the GZipStream for output to the dstFile
    GZipStream gzip = null;
    byte[] buffer;
    int count = 0;

    try
    {
        fsOut = new FileStream(dstFile, FileMode.Create, FileAccess.Write, FileShare.None);
        gzip = new GZipStream(fsOut, CompressionMode.Compress, true);

        fsIn = new FileStream(srcFile, FileMode.Open, FileAccess.Read, FileShare.Read);
        buffer = new byte[fsIn.Length];
        count = fsIn.Read(buffer, 0, buffer.Length);
        fsIn.Close();
        fsIn = null;

        // compress to the destination file
        gzip.Write(buffer, 0, buffer.Length);
    }
    catch (Exception ex)
    {
        // handle or display the error
        System.Diagnostics.Debug.Assert(false, ex.ToString());
    }
    finally
    {
        if (gzip != null)
        {
            gzip.Close();
            gzip = null;
        }
        if (fsOut != null)
        {
            fsOut.Close();
            fsOut = null;
        }
        if (fsIn != null)
        {
            fsIn.Close();
            fsIn = null;
        }
    }
}


private void TestDecompress()
{
    string srcFile = "C://temp//compressed-file.gzip";
    string dstFile = "C://temp//decompressed-file.txt";

    FileStream fsIn = null; // will open and read the srcFile
    FileStream fsOut = null; // will be used by the GZipStream for output to the dstFile
    GZipStream gzip = null;
    const int bufferSize = 4096;
    byte[] buffer = new byte[bufferSize];
    int count = 0;

    try
    {
        
        fsIn = new FileStream(srcFile, FileMode.Open, FileAccess.Read, FileShare.Read);
        fsOut = new FileStream(dstFile, FileMode.Create, FileAccess.Write, FileShare.None);
        gzip = new GZipStream(fsIn, CompressionMode.Decompress, true);
        while (true)
        {
            count = gzip.Read(buffer, 0, bufferSize);
            if (count != 0)
            {
                fsOut.Write(buffer, 0, count);
            }
            if (count != bufferSize)
            {
                // have reached the end
                break;
            }
        }
    }
    catch (Exception ex)
    {
        // handle or display the error
        System.Diagnostics.Debug.Assert(false, ex.ToString());
    }
    finally
    {
        if (gzip != null)
        {
            gzip.Close();
            gzip = null;
        }
        if (fsOut != null)
        {
            fsOut.Close();
            fsOut = null;
        }
        if (fsIn != null)
        {
            fsIn.Close();
            fsIn = null;
        }
    }
}

引用提到了一个错误信息: undefined reference to `compress2'。 这个错误信息表明在链接过程中找不到对`compress2`函数的引用。这通常发生在编译器找不到相应的库文件或者链接器无法解析对应的符号时。 从和的引用中可以看到,这个错误信息出现在libgetfea.so库中。根据引用中提到的,apache.commons.compress是一个第三方开源软件,可以用于解压和压缩文件。所以,可能的解决方法是使用apache.commons.compress库中的相应函数来替代`compress2`函数。 建议你检查项目的依赖关系和链接库设置,确保正确地引入了apache.commons.compress库,并且在编译和链接过程中正确地使用了对应的函数。同时,还可以查看相关文档或者搜索引擎,了解更多关于`compress2`函数的信息和用法,以便更好地解决这个问题。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* [GetFeature程序平台移植问题解决](https://blog.csdn.net/zhrh0096/article/details/11558115)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 50%"] - *3* [commons-compress包](https://download.csdn.net/download/wyyother1/60263144)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值