導航:首頁 > 文件處理 > zlib壓縮

zlib壓縮

發布時間:2022-01-15 10:04:44

⑴ qemu 為什麼採用zlib壓縮演算法

不同類型的文件可以選擇不同的壓縮演算法 圖片壓縮演算法 文字壓縮演算法 視頻壓縮演算法等 選擇正確的演算法,能夠提高壓縮率

python中如何對文件進行 zlib壓縮

文件讀取以後也是一個大的字元串,整個一起壓縮就可以了。

示例:

fin=open('in.txt','r')
fout=open('out.txt','w')
str=fin.read()
//compressstr
fout.write(compressed_str)
fout.close()
fin.close()

⑶ 什麼是zlib壓縮

看這里,

http://www.a2605.org/ddvipcom/program/vc/index15/5.htm

⑷ 關於zlib解壓縮的問題~

壓縮與解壓縮的時候,分別有2個不同的版本,分別是safe和普通的版本。2個版本要對應起來。

你在解壓縮的時候,注意緩沖區大小了嗎?緩沖區夠用了嗎?在壓縮前,保存一下這個壓縮前的原始的長度,然後解壓前,分配一塊至少這么大的內存。

你實際調試過嗎?比如,你可以先去掉文件IO的過程,只是對一個字元串進行壓縮/解壓,然後看看是否正確;然後再加上文件IO,看看存取的過程是否正確。壓縮後的文件應該以二進制方式打開對吧。

⑸ 如何發揮zlib壓縮解壓的最大效

首先說明,這里不是橫向比較zlib與別的引擎(rar,leo,powerarc...),是探索如何發揮zlib壓縮/解壓的最大效率。
先看看如下代碼在效率上的差異:
var MS:TMemoryStream;(1):begin MS:=TMemoryStream.Create; MS.Size:=$400000;//4M------------------------------------------------(2):var i:integer;begin MS:=TMemoryStream.Create; for i:=1 to 1024 do MS.Size:=MS.Size+4096;

你會發現,方法(1)只要1個毫秒,方法(2)卻要20秒。
因此,如果把解壓縮程序寫成下面這樣,會非常沒有效率:
procere ZlibDeCompress(instream,outStream:TStream);var ACS:TDeCompressionStream; buf:array[1..4096] of byte; numread:integer;begin inStream.Position:=0; ACS:=TDeCompressionStream.Create(inStream); try repeat numRead:=ACS.Read(buf,sizeof(buf)); if numread>0 then outStream.Write(buf,numRead); until (numRead=0); finally ACS.Free; end;end;

如果我們知道原始資料的大小,一次確定outStream.Size,效率就可以提高幾十倍。方法很簡單,我們可以在壓縮時,把原始資料的Size寫在壓縮Stream的頭部,如,寫一個LongWord的大小,解壓時就可以先讀出Size,因此,最有效率的解壓程序為:
procere ZlibDecompressStream2(Source,Dest:TMemoryStream);var zstream: TZStreamRec; SourceLen,DestLen:LongWord;begin FillChar(zstream,SizeOf(TZStreamRec),0); SourceLen:=Source.Size; Source.Position:=0; Source.Read(DestLen,SizeOf(LongWord)); Dest.Size:=DestLen; zstream.next_in:=Pointer(LongWord(Source.Memory)+SizeOf(LongWord)); zstream.avail_in:=SourceLen-SizeOf(LongWord); zstream.next_out:=Dest.Memory; zstream.avail_out:=DestLen; ZDecompressCheck(InflateInit(zstream)); try ZDecompressCheck(inflate(zstream,Z_NO_FLUSH)); finally ZDecompressCheck(inflateEnd(zstream)); end;end;

用一個4M的文件試試,效率提高近70倍。
同樣道理,在壓縮的時候,如果能預先知道壓縮後的大小,也能提高效率不少,但這似乎是不可能的,也不能盲目的給outStream.Size一個"足夠大"的數值,只能按引擎的原理估算一個最接近的數值,zlib推薦的為:
((SourceLen+(SourceLen div 10)+12)+255) and not 255
因此,最有效率的壓縮程序為:
procere ZlibCompressStream2(Source,Dest:TMemoryStream; CompressLevel:TZCompressi);var zstream: TZStreamRec; SourceLen,DestLen:LongWord;begin FillChar(zstream,SizeOf(TZStreamRec),0); SourceLen:=Source.Size; DestLen:=SizeOf(LongWord)+((SourceLen+(SourceLen div 10)+12)+255) and not 255; Dest.Size:=DestLen; Dest.Position:=0; Dest.Write(SourceLen,Sizeof(LongWord)); zstream.next_in:=Source.Memory; zstream.avail_in:=SourceLen; zstream.next_out:=Pointer(LongWord(Dest.Memory)+SizeOf(LongWord)); zstream.avail_out:=DestLen-SizeOf(longWord); ZCompressCheck(DeflateInit(zstream,ZLevels[CompressLevel])); try ZCompressCheck(deflate(zstream,Z_FINISH)); finally ZCompressCheck(deflateEnd(zstream)); end; Dest.Size:=zstream.total_out+SizeOf(LongWord);end;

⑹ 關於zlib解壓縮問題,應該是zlib壓縮的吧,對壓縮不是很懂

1.不是gzip格式,0x1f8b,31,139頭不對
2.不是zlib格式
0 1 +---+---+
|CMF|FLG| (more-->)
+---+---+

incorrect header check
3.LZ自己檢查是不是純DEFLATE格式的數據吧,格式如下
|BFINAL| BTYPE | 數……據|
 BFINAL:1bit位。
0 - 還有後續子塊;
1 - 該子塊是最後一塊。
 BTYPE:2bit位。
00 - 不壓縮;
01 - 靜態Huffman編碼壓縮;
10 - 動態Huffman編碼壓縮;
11 - 保留。

⑺ zip、bzip、lzma和ZLib如果極限壓縮的話,那種壓縮率最高除這幾種之外,還有沒有壓縮率更高的壓縮方式

LZMA和ZLIB壓縮測試:
輸出結果:
zlib壓縮:255ms size:5.08MB
zlib解壓:12ms
lzma壓縮:1974ms size:5.11MB
lzma解壓:399ms
LZMA.AS解壓:27381ms
這結果真讓人大失所望,不知道是不是測試的有問題,沒有更小,反而更大了。而且解壓時間長了幾十倍。as版的LZMA解壓時間更是無法接受。還是繼續用zlib吧。

ZLIB最高

⑻ zlib 怎麼壓縮為deflate格式

先來看看 zlib 都提供了那些函數, 都在zlib.h中,看到一堆宏不要暈,其實都是為了兼容各種編譯器和一些類型定義.死死抓住那些主要的函數的原型聲明就不會受到這些東西的影響了.

關鍵的函數有那麼幾個:

(1)int compress (Bytef *dest, uLongf *destLen, const Bytef *source, uLong sourceLen);

把源緩沖壓縮成目的緩沖, 就那麼簡單, 一個函數搞定

(2) int compress2 (Bytef *dest, uLongf *destLen,const Bytef *source, uLong sourceLen,int level);

功能和上一個函數一樣,都一個參數可以指定壓縮質量和壓縮數度之間的關系(0-9)不敢肯定這個參數的話不用太在意它,明白一個道理就好了: 要想得到高的壓縮比就要多花時間

(3) uLong compressBound (uLong sourceLen);

計算需要的緩沖區長度. 假設你在壓縮之前就想知道你的產度為 sourcelen 的數據壓縮後有多大, 可調用這個函數計算一下,這個函數並不能得到精確的結果,但是它可以保證實際輸出長度肯定小於它計算出來的長度

(4) int uncompress (Bytef *dest, uLongf *destLen,const Bytef *source, uLong sourceLen);

解壓縮(看名字就知道了:)

(5) deflateInit() + deflate() + deflateEnd()

3個函數結合使用完成壓縮功能,具體用法看 example.c 的 test_deflate()函數. 其實 compress() 函數內部就是用這3個函數實現的(工程 zlib 的 compress.c 文件)

(6) inflateInit() + inflate() + inflateEnd()

和(5)類似,完成解壓縮功能.

(7) gz開頭的函數. 用來操作*.gz的文件,和文件stdio調用方式類似. 想知道怎麼用的話看example.c 的 test_gzio() 函數,很easy.

(8) 其他諸如獲得版本等函數就不說了.

總結: 其實只要有了compress() 和uncompress() 兩個函數,在大多數應用中就足夠了.

⑼ 如何用zlib解壓gzip數據

class CGZIP
{
public:

LPGZIP pgzip;

int Length;
public:

CGZIP(char* lpsz, Memory *pool, int len = -1):pgzip(0),Length(0)

{

this->m_pool = pool;

Init(lpsz,len);

}

~CGZIP()

{

if(pgzip!=m_buffer) TRYFREE(pgzip);

}

void Init(char *lpsz,int len=-1)

{

if(lpsz==0)

{

pgzip=0;

Length=0;

return ;

}

if(len==-1)

{

len=(int)strlen(lpsz);

}

m_CurrentBufferSize=BufLength;

pgzip=m_buffer;

m_zstream.zalloc = (alloc_func)0;

m_zstream.zfree = (free_func)0;

m_zstream.opaque = (voidpf)0;

m_zstream.next_in = Z_NULL;

m_zstream.next_out = Z_NULL;

m_zstream.avail_in = 0;

m_zstream.avail_out = 0;

m_z_err = Z_OK;

m_crc = crc32(0L, Z_NULL, 0);

int err = deflateInit2(&(m_zstream), t_nLevel,Z_DEFLATED, -MAX_WBITS, DEF_MEM_LEVEL, t_nStrategy);

m_outbuf = (Byte*)ALLOC(Z_BUFSIZE);

m_zstream.next_out = m_outbuf;

if (err != Z_OK || m_outbuf == Z_NULL)

{

destroy();

return ;

}

m_zstream.avail_out = Z_BUFSIZE;

GZIP header[10]={0x1f,0x8b,Z_DEFLATED, 0 /*flags*/, 0,0,0,0 /*time*/, 0 /*xflags*/, OS_CODE};

write(header,10);

m_zstream.next_in = (Bytef*)lpsz;

m_zstream.avail_in = len;

while (m_zstream.avail_in != 0)

{

if (m_zstream.avail_out == 0)

{

m_zstream.next_out = m_outbuf;

this->write(m_outbuf,Z_BUFSIZE);

m_zstream.avail_out = Z_BUFSIZE;

}

m_z_err = deflate(&m_zstream,Z_NO_FLUSH);

if (m_z_err != Z_OK) break;

}

m_crc = crc32(m_crc, (const Bytef *)lpsz, len);

if (finish() != Z_OK) { destroy(); return ;}

putLong(m_crc);

putLong (m_zstream.total_in);

destroy();

}
private:

GZIP m_buffer[BufLength];

int m_CurrentBufferSize;

Memory *m_pool;

z_stream m_zstream;

int m_z_err; /* error code for last stream operation */

Byte *m_outbuf; /* output buffer */

uLong m_crc; /* crc32 of uncompressed data */

int write(LPGZIP buf,int count)

{

if(buf==0) return 0;

if(Length+count>m_CurrentBufferSize)

{

int nTimes=(Length+count)/BufLength +1;

LPGZIP pTemp=pgzip;

pgzip=static_cast<LPGZIP>( malloc(nTimes*BufLength));

m_CurrentBufferSize=nTimes*BufLength;

System::IO::Memory::memcpy(pgzip,pTemp,Length);

if(pTemp!=m_buffer) free(pTemp);

}

System::IO::Memory::memcpy(pgzip+Length,buf,count);

Length+=count;

return count;

}

int finish()

{

uInt len;

int done = 0;

m_zstream.avail_in = 0;

for (;;)

{

len = Z_BUFSIZE - m_zstream.avail_out;

if (len != 0)

{

write(m_outbuf,len);

m_zstream.next_out = m_outbuf;

m_zstream.avail_out = Z_BUFSIZE;

}

if (done) break;

m_z_err = deflate(&(m_zstream), Z_FINISH);

if (len == 0 && m_z_err == Z_BUF_ERROR) m_z_err = Z_OK;

done = (m_zstream.avail_out != 0 || m_z_err == Z_STREAM_END);

if (m_z_err != Z_OK && m_z_err != Z_STREAM_END) break;

}

return m_z_err == Z_STREAM_END ? Z_OK : m_z_err;

}

int destroy()

{

int err = Z_OK;

if (m_zstream.state != NULL) {

err = deflateEnd(&(m_zstream));

}

if (m_z_err < 0) err = m_z_err;

TRYFREE(m_outbuf);

return err;

}

void putLong (uLong x)

{

for(int n = 0; n < 4; n++) {

unsigned char c=(unsigned char)(x & 0xff);

write(&c,1);

x >>= 8;

}

}
};
class UNGZIP
{
public:

char *psz;

int Length;

UNGZIP(LPGZIP pgzip, int len, Memory *pool):m_gzip(pgzip),m_gziplen(len),psz(0),Length(0),m_pos(0)

{

this->m_pool = pool;

Init();

}

~UNGZIP()

{

if(psz!=m_buffer) TRYFREE(psz);

}

void Init()

{

if(m_gzip==0)

{

psz=0;

Length=0;

return ;

}

m_CurrentBufferSize=BufLength;

psz=m_buffer;

System::IO::Memory::memset(psz,0,m_CurrentBufferSize+1);

m_zstream.zalloc = (alloc_func)0;

m_zstream.zfree = (free_func)0;

m_zstream.opaque = (voidpf)0;

m_zstream.next_in = m_inbuf = Z_NULL;

m_zstream.next_out = Z_NULL;

m_zstream.avail_in = m_zstream.avail_out = 0;

m_z_err = Z_OK;

m_z_eof = 0;

m_transparent = 0;

m_crc = crc32(0L, Z_NULL, 0);

m_zstream.next_in =m_inbuf = (Byte*)ALLOC(Z_BUFSIZE);

int err = inflateInit2(&(m_zstream), -MAX_WBITS);

if (err != Z_OK || m_inbuf == Z_NULL)

{

destroy();

return;

}

m_zstream.avail_out = Z_BUFSIZE;

check_header();

char outbuf[Z_BUFSIZE];

int nRead;

while((nRead = gzread(outbuf,Z_BUFSIZE)) > 0)

{

write(outbuf,nRead);

}

destroy();

}
private:

char m_buffer[BufLength+1];

int m_CurrentBufferSize;

Memory *m_pool;

z_stream m_zstream;

int m_z_err; /* error code for last stream operation */

Byte *m_inbuf; /* output buffer */

uLong m_crc; /* crc32 of uncompressed data */

int m_z_eof;

int m_transparent;

int m_pos;

LPGZIP m_gzip;

int m_gziplen;

void check_header()

{

int method; /* method byte */

int flags; /* flags byte */

uInt len;

int c;

/* Check the gzip magic header */

for (len = 0; len < 2; len++) {

c = get_byte();

if (c != gz_magic[len]) {

if (len != 0) m_zstream.avail_in++, m_zstream.next_in--;

if (c != EOF) {

m_zstream.avail_in++, m_zstream.next_in--;

m_transparent = 1;

}

m_z_err =m_zstream.avail_in != 0 ? Z_OK : Z_STREAM_END;

return;

}

}

method = get_byte();

flags = get_byte();

if (method != Z_DEFLATED || (flags & RESERVED) != 0) {

m_z_err = Z_DATA_ERROR;

return;

}

/* Discard time, xflags and OS code: */

for (len = 0; len < 6; len++) (void)get_byte();

if ((flags & EXTRA_FIELD) != 0) { /* skip the extra field */

len = (uInt)get_byte();

len += ((uInt)get_byte())<<8;

/* len is garbage if EOF but the loop below will quit anyway */

while (len-- != 0 && get_byte() != EOF) ;

}

if ((flags & ORIG_NAME) != 0) { /* skip the original file name */

while ((c = get_byte()) != 0 && c != EOF) ;

}

if ((flags & COMMENT) != 0) { /* skip the .gz file comment */

while ((c = get_byte()) != 0 && c != EOF) ;

}

if ((flags & HEAD_CRC) != 0) { /* skip the header crc */

for (len = 0; len < 2; len++) (void)get_byte();

}

m_z_err = m_z_eof ? Z_DATA_ERROR : Z_OK;

}

int get_byte()

{

if (m_z_eof) return EOF;

if (m_zstream.avail_in == 0)

{

errno = 0;

m_zstream.avail_in =read(m_inbuf,Z_BUFSIZE);

if(m_zstream.avail_in == 0)

{

m_z_eof = 1;

return EOF;

}

m_zstream.next_in = m_inbuf;

}

m_zstream.avail_in--;

return *(m_zstream.next_in)++;

}

int read(LPGZIP buf,int size)

{

int nRead=size;

if(m_pos+size>=m_gziplen)

{

nRead=m_gziplen-m_pos;

}

if(nRead<=0) return 0;

System::IO::Memory::memcpy(buf,m_gzip+m_pos,nRead);

m_pos+=nRead;

return nRead;

}

int gzread(char* buf,int len)

{

Bytef *start = (Bytef*)buf; /* starting point for crc computation */

Byte *next_out; /* == stream.next_out but not forced far (for MSDOS) */

if (m_z_err == Z_DATA_ERROR || m_z_err == Z_ERRNO) return -1;

if (m_z_err == Z_STREAM_END) return 0; /* EOF */

next_out = (Byte*)buf;

m_zstream.next_out = (Bytef*)buf;

m_zstream.avail_out = len;

while (m_zstream.avail_out != 0) {

if (m_transparent)

{

/* Copy first the lookahead bytes: */

uInt n = m_zstream.avail_in;

if (n > m_zstream.avail_out) n = m_zstream.avail_out;

if (n > 0)

{

zmemcpy(m_zstream.next_out,m_zstream.next_in, n);

next_out += n;

m_zstream.next_out = next_out;

m_zstream.next_in += n;

m_zstream.avail_out -= n;

m_zstream.avail_in -= n;

}

if (m_zstream.avail_out > 0) {

m_zstream.avail_out -=read(next_out,m_zstream.avail_out);

}

len -= m_zstream.avail_out;

m_zstream.total_in += (uLong)len;

m_zstream.total_out += (uLong)len;

if (len == 0) m_z_eof = 1;

return (int)len;

}

if (m_zstream.avail_in == 0 && !m_z_eof)

{

errno = 0;

m_zstream.avail_in = read(m_inbuf,Z_BUFSIZE);

if (m_zstream.avail_in == 0)

{

m_z_eof = 1;

}

m_zstream.next_in = m_inbuf;

}

m_z_err = inflate(&(m_zstream), Z_NO_FLUSH);

if (m_z_err == Z_STREAM_END)

{

/* Check CRC and original size */

m_crc = crc32(m_crc, start, (uInt)(m_zstream.next_out - start));

start = m_zstream.next_out;

if (getLong() != m_crc) {

m_z_err = Z_DATA_ERROR;

}else

{

(void)getLong();

check_header();

if (m_z_err == Z_OK)

{

uLong total_in = m_zstream.total_in;

uLong total_out = m_zstream.total_out;

inflateReset(&(m_zstream));

m_zstream.total_in = total_in;

m_zstream.total_out = total_out;

m_crc = crc32(0L, Z_NULL, 0);

}

}

}

if (m_z_err != Z_OK || m_z_eof) break;

}

m_crc = crc32(m_crc, start, (uInt)(m_zstream.next_out - start));

return (int)(len - m_zstream.avail_out);

}

uLong getLong()

{

uLong x = (uLong)get_byte();

int c;

x += ((uLong)get_byte())<<8;

x += ((uLong)get_byte())<<16;

c = get_byte();

if (c == EOF) m_z_err = Z_DATA_ERROR;

x += ((uLong)c)<<24;

return x;

}

int write(char* buf,int count)

{

if(buf==0) return 0;

if(Length+count>m_CurrentBufferSize)

{

int nTimes=(Length+count)/BufLength +1;

char *pTemp=psz;

psz=static_cast<char*>( malloc(nTimes*BufLength+1));

m_CurrentBufferSize=nTimes*BufLength;

Memory::memset(psz,0,m_CurrentBufferSize+1);

Memory::memcpy(psz,pTemp,Length);

if(pTemp!=m_buffer) free(pTemp);

}

Memory::memcpy(psz+Length,buf,count);

Length+=count;

return count;

}

int destroy()

{

int err = Z_OK;

if (m_zstream.state != NULL) {

err = inflateEnd(&(m_zstream));

}

if (m_z_err < 0) err = m_z_err;

TRYFREE(m_inbuf);

return err;

}
};

⑽ 如何用zlib將很多文件或一個文件夾壓縮

問題的根源在於這些網友對於字元串和位元組流的概念非常的模糊,對文本文件和二進制文件的區別常常模稜兩可,其實位元組流可以表示所有的數據,二進制文件才是任何文件的本質。位元組流是一個位元組接一個位元組,並沒有結束符號

閱讀全文

與zlib壓縮相關的資料

熱點內容
cad命令縮寫 瀏覽:878
論文摘要寫什麼app 瀏覽:490
懷孕可以吃什麼app 瀏覽:387
不可以使用決策樹演算法 瀏覽:29
電腦單片機燒錄顯示正在檢測 瀏覽:530
柱筋搭接箍筋必須加密嗎 瀏覽:770
輿情pdf 瀏覽:325
手機如何開啟蘋果手機上的app 瀏覽:769
微雲圖片可以直接壓縮嗎 瀏覽:370
程序員30歲圖 瀏覽:30
python匹配豆瓣 瀏覽:439
大金空調怎麼判斷變頻壓縮機壞 瀏覽:255
安卓手機如何注冊傳說對決 瀏覽:951
自學應聘程序員 瀏覽:917
linux設置根目錄 瀏覽:89
java向量空間模型 瀏覽:841
哪個app訂餐便宜 瀏覽:672
有什麼app可能存聊天記錄 瀏覽:468
強國app中的紀錄片在哪裡 瀏覽:714
dell伺服器可用什麼樣硬碟 瀏覽:636