不同类型的文件可以选择不同的压缩算法 图片压缩算法 文字压缩算法 视频压缩算法等 选择正确的算法,能够提高压缩率
⑵ 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将很多文件或一个文件夹压缩
问题的根源在于这些网友对于字符串和字节流的概念非常的模糊,对文本文件和二进制文件的区别常常模棱两可,其实字节流可以表示所有的数据,二进制文件才是任何文件的本质。字节流是一个字节接一个字节,并没有结束符号