LZMA SDK 9.20(与C相关)

1.概述

lzma是7-Zip压缩项目中7z格式的默认与通用压缩方法,压缩率高,解压速度快,需要的内存小。目录中包括ANSI-C/C++/C#/Java源码,以及用于windows的编译好的文件,以及几个文档:

  • lzma.txt - LZMA SDK描述文件
  • 7zFormat.txt - 7z格式描述文件
  • 7zC.txt - 7z ANSI-C解码描述文件
  • methods.txt - .7z文件的压缩方式
  • lzma.exe - 编译好的文件,用于windows下文件到文件的解压缩
  • 7zr.exe - 编译好的7-Zip,支持7z/lzma/xz等格式
  • history.txt - LZMA SDK的历史版本

2.C文件

C文件夹下:
- 7zCrc*.* - CRC code
- Alloc.* - Memory allocation functions
- Bra*.* - Filters for x86, IA-64, ARM, ARM-Thumb, PowerPC and SPARC code
- LzFind.* - Match finder for LZ (LZMA) encoders
- LzFindMt.* - Match finder for LZ (LZMA) encoders for multithreading encoding
- LzHash.h - Additional file for LZ match finder
- LzmaDec.* - LZMA decoding
- LzmaEnc.* - LZMA encoding
- LzmaLib.* - LZMA Library for DLL calling
- Types.h - Basic types for another .c files
- Threads.* - The code for multithreading.

Util文件夹下:

  • LzmaLib - LZMA库 (.DLL for Windows)
  • Lzma - LZMA使用方法 (文件到文件的 LZMA encoder/decoder).
  • 7z - 7z ANSI-C 解码器

3.用法

LZMA <e|d> inputFile outputFile [<switches>...]

e: encode file

d: decode file

<Switches>
  -a{N}:  压缩模式 0 = fast, 1 = normal
          default: 1 (normal)

  -d{N}:  字典大小 - [0, 30], default: 23 (8MB)
          字典大小最大值为 1 GB = 2^30 bytes.
          计算方式为 DictionarySize = 2^N bytes. 
          对于用LZMA压缩的文件解压,字典大小为 D = 2^N 就需要 D bytes 内存(RAM)

  -fb{N}: fast bytes 数 - [5, 273], default: 128
          这个数越大,压缩比越大,速度也越慢

  -lc{N}: literal context bits 数 - [0, 8], default: 3
          对于大文件,通常设置 lc=4.

  -lp{N}: literal pos bits 数 - [0, 4], default: 0
          当周期为 2^N 时,预留给周期数据. 
          例如,对于 32-bit (4 bytes),可以设 lp=2. 
          如果这里有变化,通常要设置 lc0.

  -pb{N}: pos bits 数 - [0, 4], default: 2
          当周期为 2^N 时,预留给周期数据.

  -mf{MF_ID}: Match Finder. Default: bt4. 
              hc* 组算法压缩比不好, 但当与fast mode(-a0)结合时速度很快.
              内存需求取决于字典大小(下表中的参数"d"). 
<
MF_ID Memory Description
bt2 d * 9.5 + 4MB Binary Tree with 2 bytes hashing.
bt3 d * 11.5 + 4MB Binary Tree with 3 bytes hashing.
bt4 d * 11.5 + 4MB Binary Tree with 4 bytes hashing.
  • 1
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
首先,你需要下载 lzma SDK,可以从这里下载:https://www.7-zip.org/sdk.html。 然后,按照以下步骤进行操作: 1. 解压缩下载的 SDK 文件到本地目录。 2. 打开命令行终端,进入 SDK 目录。 3. 进入 C 目录,执行 makefile 生成库文件。在命令行中输入以下命令: ``` cd C make -f makefile.gcc ``` 4. 在编写代码之前,你需要了解 lzma SDK 的基本概念和 API 函数。你可以查看 SDK 中的文档或者示例代码来学习。 5. 在你的代码中引入 lzma 库文件,例如: ``` #include <lzma.h> ``` 6. 使用 lzma SDK 中提供的 API 函数来压缩文件。以下是一个简单的示例: ``` #include <stdio.h> #include <stdlib.h> #include <lzma.h> int main(int argc, char *argv[]) { // 打开输入文件 FILE *input = fopen(argv[1], "rb"); if (!input) { fprintf(stderr, "Error: cannot open input file.\n"); return 1; } // 打开输出文件 FILE *output = fopen(argv[2], "wb"); if (!output) { fclose(input); fprintf(stderr, "Error: cannot open output file.\n"); return 1; } // 初始化压缩器 lzma_stream strm = LZMA_STREAM_INIT; lzma_ret ret = lzma_easy_encoder(&strm, LZMA_PRESET_DEFAULT, LZMA_CHECK_CRC64); if (ret != LZMA_OK) { fclose(input); fclose(output); fprintf(stderr, "Error: cannot initialize encoder.\n"); return 1; } // 压缩文件 uint8_t inbuf[LZMA_IN_BUF_SIZE]; uint8_t outbuf[LZMA_OUT_BUF_SIZE]; size_t in_bytes, out_bytes; while ((in_bytes = fread(inbuf, 1, LZMA_IN_BUF_SIZE, input)) > 0) { strm.next_in = inbuf; strm.avail_in = in_bytes; do { strm.next_out = outbuf; strm.avail_out = LZMA_OUT_BUF_SIZE; ret = lzma_code(&strm, LZMA_FINISH); out_bytes = LZMA_OUT_BUF_SIZE - strm.avail_out; fwrite(outbuf, 1, out_bytes, output); } while (strm.avail_out == 0); } // 结束压缩 lzma_end(&strm); // 关闭文件 fclose(input); fclose(output); return 0; } ``` 在命令行中输入以下命令来编译代码: ``` gcc -o compress compress.c liblzma.a ``` 其中,compress.c 是你的代码文件名,liblzma.a 是 lzma SDK 提供的库文件。执行以下命令来压缩文件: ``` ./compress input_file output_file ``` 其中,input_file 是你要压缩的文件名,output_file 是压缩后的文件名。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值