C++使用 LZ4 进行压缩,压缩信息不符合预期
C++ compressing using lz4, compressed information not as expected
我在Mac上使用lz4,并做了一个实验来压缩程序中的字符串(名为str)。
#include <fstream>
#include <iostream>
#include "lz4.h"
using namespace std;
int main(){
char str[] = "10100100010000100000100000010000000100000000100000000010000000000";
size_t len = sizeof(str);
char* target = new char[len];
int nCompressedSize = LZ4_compress_default((const char *)(&str), target, len, len);
ofstream os("lz4.dat",ofstream::binary);
os.write(target, nCompressedSize);
os.close();
delete[] target;
target = 0;
ifstream is( "lz4.dat", ifstream::binary );
is.seekg (0,is.end);
size_t nCompressedInputSize = is.tellg();
is.clear();
is.seekg(0,ios::beg);
//Read file into buffer
char* in = new char[nCompressedInputSize];
int32_t n=is.read(in,nCompressedSize);
cout<<"Byte number:"<<nCompressedSize<<",file size:"<<n<<",bytes read:"<<in<<endl;
is.close();
return 0;
}
运行这个程序,我检查了"lz4.dat"文件:
$ls -lrt lz4.dat
-rw-r--r-- 1 x staff 34 7 15 14:50 lz4.dat
它是 34 字节,好的,但程序输出是:
Byte number:34,file size:1,bytes read:@1010
很奇怪,似乎收到的文件大小是 1 字节,我实际上输出了一些随机 @1010。为什么我的"is.tellg()"没有得到正确的文件长度?
谢谢。
ifstream::read()
不返回读取的字节数。它返回对*this
的引用,它有operator bool()
,我认为用于 case。所以你在n
,你得到操作是否成功。
输出似乎完全没问题,它是压缩数据的开始。我认为只打印了几个字节,因为它包含一个终止零。它类似于您的输入,因为 lz4 将文字逐字放入流中(lz4 没有熵编码)
相关文章:
- 正在查找文档以获得PS4平台的C++中的设备信息
- C++中高效的大型稀疏块压缩线性方程
- 嵌入方指针压缩已禁用
- C++使用整数的压缩数组初始化对象
- 为什么在运行时没有向我们提供有关分段错误的更多信息?
- 奇怪的(对我来说)返回声明 - 在谷歌上找不到任何关于它的信息
- 在C++中将函数压缩为两种方式
- 如何设置一个范围来提取我想要获得的信息
- 在C++中使用LZ4压缩目录
- 使用C++进行运行长度解压缩
- 为什么大多数 pair 实现默认不使用压缩(空基优化)?
- 系统参数信息A 与 SPI_GETMOUSE 返回 0
- 为什么 gcovr 会生成空覆盖率统计信息?
- 捕获标准输出以压缩并使用 CTRL-C 中断会给出损坏的 zip 文件
- C++ 如何将数组值解压缩为函数参数
- struct.error:解压缩 C++ 结构时,解包需要 288 字节的缓冲区
- 在 Qt(C++) 中使用 QProcess 解压缩 - 提取目录问题
- 我已经阅读了很多关于 2d 数组的信息,但我在作业中使用它时遇到了麻烦
- 浏览压缩文件与游览解压缩它们
- C++使用 LZ4 进行压缩,压缩信息不符合预期