压缩 解压文件

linux 压缩解压文件

Linux压缩和解压工具,常用的压缩格式后缀:

  • tar.gz
    --压缩:  tar -zcvf 压缩后的包名.tar.gz  压缩的目标
    --解压:  tar -zxvf 压缩后的包名.tar.gz [-C 目标目录]
    --查看:  tar -ztvf | grep ''
    --例子:
            # tar -zcvf varlog.tar.gz /var/log/
            # tar -zxf varlog.tar.gz -C target/ 

    -z 压缩成gz后缀
    -c 创建
    -x 解压
    -v 显示过程
    -f 接文档名  之后不跟任何参数
  • tar.bz2
    --压缩:  tar -jcvf 压缩后的包名.tar.bz2  压缩的目标
    --解压:  tar -jxvf 压缩后的包名.tar.bz2 [-C 目标目录]
  • zip
    --压缩:  zip  压缩的目标
    --解压:  unzip  压缩包
  • rar
    linux 默认不支持,需要安装第三方插件
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在 Linux C++ 中进行文件压缩解压缩,可以使用 zlib 库。该库提供了一组函数,可以在 C++ 中使用。 以下是一个简单的示例代码,展示了如何使用 zlib 库进行文件压缩解压缩: ```c++ #include <iostream> #include <fstream> #include <string> #include <zlib.h> // 压缩文件 void compressFile(const std::string& srcFile, const std::string& dstFile) { std::ifstream ifs(srcFile.c_str(), std::ios::binary); std::ofstream ofs(dstFile.c_str(), std::ios::binary); if (!ifs || !ofs) { std::cerr << "open file failed" << std::endl; return; } // 初始化 zlib z_stream strm; strm.zalloc = Z_NULL; strm.zfree = Z_NULL; strm.opaque = Z_NULL; if (deflateInit(&strm, Z_DEFAULT_COMPRESSION) != Z_OK) { std::cerr << "deflateInit failed" << std::endl; return; } // 压缩数据 char in[1024]; char out[1024]; do { ifs.read(in, sizeof(in)); strm.avail_in = ifs.gcount(); strm.next_in = reinterpret_cast<Bytef*>(in); do { strm.avail_out = sizeof(out); strm.next_out = reinterpret_cast<Bytef*>(out); deflate(&strm, Z_FINISH); ofs.write(out, sizeof(out) - strm.avail_out); } while (strm.avail_out == 0); } while (!ifs.eof()); // 结束压缩 deflateEnd(&strm); } // 解压文件 void uncompressFile(const std::string& srcFile, const std::string& dstFile) { std::ifstream ifs(srcFile.c_str(), std::ios::binary); std::ofstream ofs(dstFile.c_str(), std::ios::binary); if (!ifs || !ofs) { std::cerr << "open file failed" << std::endl; return; } // 初始化 zlib z_stream strm; strm.zalloc = Z_NULL; strm.zfree = Z_NULL; strm.opaque = Z_NULL; if (inflateInit(&strm) != Z_OK) { std::cerr << "inflateInit failed" << std::endl; return; } // 解压数据 char in[1024]; char out[1024]; do { ifs.read(in, sizeof(in)); strm.avail_in = ifs.gcount(); strm.next_in = reinterpret_cast<Bytef*>(in); do { strm.avail_out = sizeof(out); strm.next_out = reinterpret_cast<Bytef*>(out); inflate(&strm, Z_NO_FLUSH); ofs.write(out, sizeof(out) - strm.avail_out); } while (strm.avail_out == 0); } while (!ifs.eof()); // 结束解压 inflateEnd(&strm); } int main() { const std::string srcFile = "test.txt"; const std::string dstFile = "test.gz"; // 压缩文件 compressFile(srcFile, dstFile); // 解压文件 uncompressFile(dstFile, "output.txt"); return 0; } ``` 在上面的代码中,compressFile 函数用于压缩文件,uncompressFile 函数用于解压文件。这两个函数都包含了对 zlib 库的初始化、数据处理和结束操作。请注意,这里使用的是 gzip 压缩格式,因此压缩文件的扩展名为 .gz。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值