我的任务是使用zlib解压缩数据包(已接收),然后使用算法从数据中生成图片
好消息是我有C++代码,但任务是在C#中完成
C++
//Read the first values of the packet received
DWORD image[200 * 64] = {0}; //used for algoritm(width always = 200 and height always == 64)
int imgIndex = 0; //used for algoritm
unsigned char rawbytes_[131072] = {0}; //read below
unsigned char * rawbytes = rawbytes_; //destrination parameter for decompression(ptr)
compressed = r.Read<WORD>(); //the length of the compressed bytes(picture)
uncompressed = r.Read<WORD>(); //the length that should be after decompression
width = r.Read<WORD>(); //the width of the picture
height …Run Code Online (Sandbox Code Playgroud) 我正在编写存储数据的项目,所以我需要压缩它.我试过zlib,但它是我项目的瓶颈.所以也许有更快的解决方案.我不需要很高的压缩率,但我正在寻找真正快速的压缩.是否有除zlib的任何其他数据压缩库,这是真正的自由,可以在专有软件中使用(项目,我的工作,是不是GPL为主).我的项目是在C++上,我需要压缩char*文本数组.
我正在将一个Python应用程序移植到Android,并且在某些时候,该应用程序必须与Web服务进行通信,并向其发送压缩数据.
为此,它使用下一个方法:
def stuff(self, data):
"Convert into UTF-8 and compress."
return zlib.compress(simplejson.dumps(data))
Run Code Online (Sandbox Code Playgroud)
我正在使用下一个方法尝试在Android中模拟此行为:
private String compressString(String stringToCompress)
{
Log.i(TAG, "Compressing String " + stringToCompress);
byte[] input = stringToCompress.getBytes();
// Create the compressor with highest level of compression
Deflater compressor = new Deflater();
//compressor.setLevel(Deflater.BEST_COMPRESSION);
// Give the compressor the data to compress
compressor.setInput(input);
compressor.finish();
// Create an expandable byte array to hold the compressed data.
// You cannot use an array that's the same size as the orginal because
// there …Run Code Online (Sandbox Code Playgroud) ZipArchive是否有一个简单的界面用于tar.gz存档,允许我将存档流式传输到客户端而不是将其保存在服务器上?如果是这样,有人可以告诉我怎么样?
谢谢!
根据MSDN的.Net 4.5 System.IO.Compression基于zlib.
我现在正在尝试将当前基于互操作的读取从非.NET服务器的zlib放气流更改为基于BCL的实现.
我的实现看起来像这样:
var enc = new UTF8Encoding();
var readBytes = BufferSizeRaw;
var outputBuffer = new byte[BufferSizeRaw];
var networkBuffer = _networkQueue.Take();
var ms = new MemoryStream(networkBuffer.InputBuffer, 0, networkBuffer.UsedLength);
using (Stream stream = new DeflateStream(ms, CompressionMode.Decompress))
while (readBytes==BufferSizeRaw)
{
readBytes = stream.Read(outputBuffer, 0, outputBuffer.Length);
stringBuffer+= enc.GetString(outputBuffer, 0, readBytes);
}
Run Code Online (Sandbox Code Playgroud)
在第一次调用DeflateStream上的解压缩/读取时收到以下异常:
块长度与其补码不匹配
基于互操作的呼叫使用var result=inflate(ref zStyream, ZLibFlush.NoFlush;
有没有人尝试相同或看到代码中的错误的原因或我的结束有错误的理解?我也尝试过截断前两个字节而没有任何运气.
前几个字节是20,202,177,13.
我有一堆json对象需要压缩,因为它占用了太多的磁盘空间,大约20 gigs价值几百万.
理想情况下,我想要做的是单独压缩每个,然后当我需要读取它们时,只需迭代加载和解压缩每个.我尝试通过创建一个文本文件来做到这一点,每个行都是通过zlib压缩的json对象,但这是失败的
decompress error due to a truncated stream,
我认为这是由于包含新行的压缩字符串.
有人知道这样做的好方法吗?
我试图加快最初通过切换到C用Java编写的我TIFF编码器和编译的zlib 1.2.8与Z_SOLO定义和最低限度的C文件:adler32.c,crc32.c,deflate.c,trees.c和zutil.c.Java正在使用java.util.zip.Deflater.
我编写了一个简单的测试程序,用于评估压缩级别和速度方面的性能,并且考虑到更高级别所需的时间越来越多,压缩不会产生太大的影响.我也惊讶于Java在压缩和速度方面的表现实际上比Visual Studio Release-compile(VC2010)更好:
Java的:
Level 1 : 8424865 => 6215200 (73,8%) in 247 cycles.
Level 2 : 8424865 => 6178098 (73,3%) in 254 cycles.
Level 3 : 8424865 => 6181716 (73,4%) in 269 cycles.
Level 4 : 8424865 => 6337236 (75,2%) in 334 cycles.
Level 5 : 8424865 => 6331902 (75,2%) in 376 cycles.
Level 6 : 8424865 => 6333914 (75,2%) …Run Code Online (Sandbox Code Playgroud) 我需要在C++中对std :: string进行简单的压缩和解压缩.我查看了这个站点,代码是用于Character数组.我想要实现的是两个功能:
std::string original = "This is to be compressed!!!!";
std::string compressed = string_compress(original);
std::cout << compressed << std::endl;
std::string decompressed = string_decompress(compressed);
std::cout << decompressed << std::endl;
Run Code Online (Sandbox Code Playgroud)
我曾尝试将boost压缩为:
std::string CompressData(const std::string &data)
{
std::stringstream compressed;
std::stringstream decompressed;
decompressed << data;
boost::iostreams::filtering_streambuf<boost::iostreams::input> out;
out.push(boost::iostreams::zlib_compressor());
out.push(decompressed);
boost::iostreams::copy(out, compressed);
return compressed.str();
}
std::string DecompressData(const std::string &data)
{
std::stringstream compressed;
std::stringstream decompressed;
compressed << data;
boost::iostreams::filtering_streambuf<boost::iostreams::input> in;
in.push(boost::iostreams::zlib_decompressor());
in.push(compressed);
boost::iostreams::copy(in, decompressed);
return decompressed.str();
}
Run Code Online (Sandbox Code Playgroud)
但是代码有时会在字符串中给出Null字符,即\ u0000.如果压缩数据包含这些空字符,我该如何处理.返回类型字符串是否正确?我怎样才能实现功能 …
我正在将项目的代码从1998版的zlib更新为2013版的zlib.似乎改变的一件事是,uncompress函数曾经有一个"use_crc"标志,似乎已经丢失了:
int ZEXPORT uncompress (dest, destLen, source, sourceLen, use_crc)
Bytef *dest;
uLongf *destLen;
const Bytef *source;
uLong sourceLen;
int use_crc; // <-- vanished (?)
Run Code Online (Sandbox Code Playgroud)
(更新:正如@Joe所指出的,这很可能是第三方修改.标题也相应更新.问题的其余部分仍然适用,例如,"我应该如何利用今天的股票zlib做到最好".)
在我正在研究的代码中,uncompress()被解析为.zip的二进制格式并传入数据的"有效负载".代码已经将crc标志传递为1.如果未使用该标志,则会获得Z_DATA_ERROR(-3).(没有use_crc标志的zlib获得Z_DATA_ERROR,就像标志为false一样.)
在实验中,我发现非常小的文件没有use_crc.然后小的计数文件交叉到"12345678901234"和之间不工作"123456789012345".原因是:这是第一个被放气而不是存储未压缩的文件(在什么拉链称为"6%"的节省)
在挣扎着让zlib接受它的选项时,我尝试了很多东西.这包括尝试16 + MAX_WBITS.似乎没有任何东西像zip test.zip test.txt那样处理旧有代码的方式.
如果我愿意从我的目的地大小中减去一个,我似乎能够抑制错误检查......丢失一个字节.这是简单的测试程序,最小拉链有效负载硬编码:
#include <stdio.h>
#include "zlib.h"
int main(int argc, char *argv[]) {
char compressed[] = { 0x78, 0x9C, 0x33, 0x34, 0x32, 0x36, 0x31, 0x35, 0x33,
0xB7, 0xB0, 0x34, 0x30, 0x04, 0xB1, 0xB8, 0x00, 0x31, 0x30, 0xB1, …Run Code Online (Sandbox Code Playgroud) 我有一个从库调用读取的字节缓冲区,我想解压缩单个文本文件的内容.
我试过了zlib,但是我收到了这个错误:
>>> import zlib
>>> zlib.decompress(buffer)
error: Error -3 while decompressing data: incorrect header check
Run Code Online (Sandbox Code Playgroud)
但ZipFile它有效,但我必须使用临时文件:
import zipfile
f = open('foo.zip', 'wb')
f.write(buffer)
f.close()
z = ZipFile('foo.zip')
z.extractall()
z.close()
with open('foo.txt', 'r') as f:
uncompressed_buffer = f.read()
Run Code Online (Sandbox Code Playgroud)
是否可以使用zlib,如何避免使用临时文件?