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

czlib解壓縮

發布時間:2022-04-28 01:03:42

⑴ 關於zlib解壓縮的問題~

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

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

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

⑵ C++中如何調用zlib.dll進行解壓和壓縮

1
准備工作。
下載zlib.dll。以及相關頭文件。將dll文件及頭文件加入工程。
2
壓縮:
調用函數compress.
形式為
int
compress(Byte
*
dest,
uLong*
destLen,
const
Byte
*source,
ULONG
sourceLen);
功能是將source指向的空間,長度為sourceLen的數據進行壓縮,壓縮數據儲存在dest中,長度由參數destLen返回。
如果壓縮出錯,返回對應錯誤號,否則返回0.
3解壓縮:
調用函數uncompress.
形式為
int
uncompress(Byte
*
dest,
uLong*
destLen,
const
Byte
*source,
ULONG
sourceLen);
功能是將source指向的空間,長度為sourceLen的數據進行解壓縮,解壓縮後的數據儲存在dest中,長度由參數destLen返回。
如果解壓縮出錯,返回對應錯誤號,否則返回0.

⑶ C++語言怎麼用zlib庫來解壓.ISO或.zip文件

下面是使用zlib庫的壓縮和解壓縮演示代碼:

#include <stdlib.h>
#include <stdio.h>
#include <zlib.h>
int main(int argc, char* argv[])
{
FILE* file;
uLong flen;
unsigned char* fbuf = NULL;
uLong clen;
unsigned char* cbuf = NULL;
/* 通過命令行參數將srcfile文件的數據壓縮後存放到dstfile文件中 */
if(argc < 3)
{
printf("Usage: zcdemo srcfile dstfile\n");
return -1;
}
if((file = fopen(argv[1], "rb")) == NULL)
{
printf("Can\'t open %s!\n", argv[1]);
return -1;
}
/* 裝載源文件數據到緩沖區 */
fseek(file, 0L, SEEK_END);    /* 跳到文件末尾 */
flen = ftell(file);        /* 獲取文件長度 */
fseek(file, 0L, SEEK_SET);
if((fbuf = (unsigned char*)malloc(sizeof(unsigned char) * flen)) == NULL)
{
printf("No enough memory!\n");
fclose(file);
return -1;
}
fread(fbuf, sizeof(unsigned char), flen, file);
/* 壓縮數據 */
clen = compressBound(flen);
if((cbuf = (unsigned char*)malloc(sizeof(unsigned char) * clen)) == NULL)
{
printf("No enough memory!\n");
fclose(file);
return -1;
}
if(compress(cbuf, &clen, fbuf, flen) != Z_OK)
{
printf("Compress %s failed!\n", argv[1]);
return -1;
}
fclose(file);
if((file = fopen(argv[2], "wb")) == NULL)
{
printf("Can\'t create %s!\n", argv[2]);
return -1;
}
/* 保存壓縮後的數據到目標文件 */
fwrite(&flen, sizeof(uLong), 1, file);    /* 寫入源文件長度 */
fwrite(&clen, sizeof(uLong), 1, file);    /* 寫入目標數據長度 */
fwrite(cbuf, sizeof(unsigned char), clen, file);
fclose(file);
free(fbuf);
free(cbuf);
return 0;
}

⑷ 為什麼用zlib.dll解壓不成功呢可以壓縮,但是不能解壓

zlib解縮時,要提供壓縮前的大小。所以一般壓縮前,要取得要壓縮數據的大小,壓縮後要自己在壓縮後的數據前加上一段自定義的數據,有於保存壓縮前的大小,以便於在解壓縮時能夠獲取壓縮前的大小。解壓縮前,可以根據自定義的這段數據,來獲取到壓縮前的大小,做為參數提供給解壓縮的api的sourceLen。

⑸ 怎麼用zlib生成標准zip壓縮文件和解壓縮zip文件

你用的是什麼壓縮軟體?7-Zip? rar是Winrar的私有格式,其它壓縮軟體通常只能打開和解壓縮,而不能生成(否則必須得到Winrar的授權)。 其它你用7z格式就可以了,Winrar等都可以打開7z(7-Zip是開源軟體)。你發給別人7z格式,他不論裝什麼壓縮.

⑹ 用C語言簡單演示如何藉助zlib庫實現文件的壓縮和解壓縮

問題的根源在於這些網友對於字元串和位元組流的概念非常的模糊,對文本文件和二進制文件的區別常常模稜兩可,其實位元組流可以表示所有的數據,二進制文件才是任何文件的本質。位元組流是一個位元組接一個位元組,並沒有結束符號,所以需要給它一個長度信息。二進制文件是一個位元組接一個位元組,並沒有換行符之類的。文件壓縮的時候,可以通過源文件的長度自動計算緩沖區的長度,壓縮後寫入目標文件之前,需先保留源文件和目標數據的長度作為解壓縮的依據,參考如下代碼:#include #include #include int main(int argc, char* argv[]) { FILE* file; uLong flen; unsigned char* fbuf = NULL; uLong clen; unsigned char* cbuf = NULL; /* 通過命令行參數將srcfile文件的數據壓縮後存放到dstfile文件中 */ if(argc < 3) { printf("Usage: zcdemo srcfile dstfile\n"); return -1; } if((file = fopen(argv[1], "rb")) == NULL) { printf("Can\'t open %s!\n", argv[1]); return -1; } /* 裝載源文件數據到緩沖區 */ fseek(file, 0L, SEEK_END); /* 跳到文件末尾 */ flen = ftell(file); /* 獲取文件長度 */ fseek(file, 0L, SEEK_SET); if((fbuf = (unsigned char*)malloc(sizeof(unsigned char) * flen)) == NULL) { printf("No enough memory!\n"); fclose(file); return -1; } fread(fbuf, sizeof(unsigned char), flen, file); /* 壓縮數據 */ clen = compressBound(flen); if((cbuf = (unsigned char*)malloc(sizeof(unsigned char) * clen)) == NULL) { printf("No enough memory!\n"); fclose(file); return -1; } if(compress(cbuf, &clen, fbuf, flen) != Z_OK) { printf("Compress %s failed!\n", argv[1]); return -1; } fclose(file); if((file = fopen(argv[2], "wb")) == NULL) { printf("Can\'t create %s!\n", argv[2]); return -1; } /* 保存壓縮後的數據到目標文件 */ fwrite(&flen, sizeof(uLong), 1, file); /* 寫入源文件長度 */ fwrite(&clen, sizeof(uLong), 1, file); /* 寫入目標數據長度 */ fwrite(cbuf, sizeof(unsigned char), clen, file); fclose(file); free(fbuf); free(cbuf); return 0; }文件解壓縮的時候,可以通過保留信息得到緩沖區和數據流的大小,這樣解壓縮後直接保存即可,參考如下代碼:#include #include #include int main(int argc, char* argv[]) { FILE* file; uLong flen; unsigned char* fbuf = NULL; uLong ulen; unsigned char* ubuf = NULL; /* 通過命令行參數將srcfile文件的數據解壓縮後存放到dstfile文件中 */ if(argc < 3) { printf("Usage: zudemo srcfile dstfile\n"); return -1; } if((file = fopen(argv[1], "rb")) == NULL) { printf("Can\'t open %s!\n", argv[1]); return -1; } /* 裝載源文件數據到緩沖區 */ fread(&ulen, sizeof(uLong), 1, file); /* 獲取緩沖區大小 */ fread(&flen, sizeof(uLong), 1, file); /* 獲取數據流大小 */ if((fbuf = (unsigned char*)malloc(sizeof(unsigned char) * flen)) == NULL) { printf("No enough memory!\n"); fclose(file); return -1; } fread(fbuf, sizeof(unsigned char), flen, file); /* 解壓縮數據 */ if((ubuf = (unsigned char*)malloc(sizeof(unsigned char) * ulen)) == NULL) { printf("No enough memory!\n"); fclose(file); return -1; } if(uncompress(ubuf, &ulen, fbuf, flen) != Z_OK) { printf("Uncompress %s failed!\n", argv[1]); return -1; } fclose(file); if((file = fopen(argv[2], "wb")) == NULL) { printf("Can\'t create %s!\n", argv[2]); return -1; } /* 保存解壓縮後的數據到目標文件 */ fwrite(ubuf, sizeof(unsigned char), ulen, file); fclose(file); free(fbuf); free(ubuf); return 0; }

⑺ delphi中用zlib怎樣壓縮和解壓

數據壓縮和解壓的示例代碼:

{壓縮流}
function CompressStream(ASrcStream: TStream; ALevel: TSfCompressionLevel): TStream;
var
SrcData,Buffer:Pointer;
BufSize:Integer;
begin
Buffer:=nil;
Result:=nil;
BufSize:=0;
GetMem(SrcData,ASrcStream.Size);
ASrcStream.Position:=0;
ASrcStream.Read(SrcData^,ASrcStream.Size);

try
try
SfCompressBuf(SrcData,ASrcStream.Size,Buffer,BufSize,ALevel);
except
on E:Exception do
SfRaiseException(E,'Exception raised in CompressStream call');
end;
finally
FreeMem(SrcData);
SrcData:=nil;
end;

//由於try...except塊中重引發了異常,所以在發生了異常的情況下,以下的代碼不會執行
Result:=TMemoryStream.Create;
Result.Write(Buffer^,BufSize);
FreeMem(Buffer);
end;

{解壓流}
function CompressStream(ASrcStream: TStream; ALevel: TSfCompressionLevel): TStream;
var
SrcData,Buffer:Pointer;
BufSize:Integer;
begin
Buffer:=nil;
Result:=nil;
BufSize:=0;
GetMem(SrcData,ASrcStream.Size);
ASrcStream.Position:=0;
ASrcStream.Read(SrcData^,ASrcStream.Size);

try
try
SfCompressBuf(SrcData,ASrcStream.Size,Buffer,BufSize,ALevel);
except
on E:Exception do
SfRaiseException(E,'Exception raised in CompressStream call');
end;
finally
FreeMem(SrcData);
SrcData:=nil;
end;

//由於try...except塊中重引發了異常,所以在發生了異常的情況下,以下的代碼不會執行
Result:=TMemoryStream.Create;
Result.Write(Buffer^,BufSize);
FreeMem(Buffer);
end;

{壓縮位元組數組}
function CompressBytes(ASrcBytes: TBytes; ALevel: TSfCompressionLevel): TBytes;
var
Buffer:Pointer;
BufSize:Integer;
begin
Buffer:=nil;
BufSize:=0;

try
SfCompressBuf(@ASrcBytes[0],Length(ASrcBytes),Buffer,BufSize,ALevel);
SetLength(Result,BufSize);
Move(Buffer^,Result[0],BufSize);
except
on E:Exception do
SfRaiseException(E,'Exception raised in CompressBytes call');
end;

//由於try...except塊中重引發了異常,所以在發生了異常的情況下,以下的代碼不會執行
FreeMem(Buffer);
end;

{解壓位元組數組}
function DecompressBytes(ASrcBytes: TBytes): TBytes;
var
Buffer:Pointer;
BufSize:Integer;
begin
Buffer:=nil;
BufSize:=0;

try
SfDecompressBuf(@ASrcBytes[0],Length(ASrcBytes),0,Buffer,BufSize);
SetLength(Result,BufSize);
Move(Buffer^,Result[0],BufSize);
except
on E:Exception do
SfRaiseException(E,'Exception raised in DecompressBytes call');
end;

//由於try...except塊中重引發了異常,所以在發生了異常的情況下,以下的代碼不會執行
FreeMem(Buffer);
end;

⑻ 如何發揮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;

java zlib 壓縮和解壓縮怎麼實現

使用java.util.zip.ZipFile 類及相關的類實現

如解壓縮
ZipInputStream zin = new ZipInputStream(in);
ZipEntry entry = null;
while((entry=zin.getNextEntry())!=null){
if(entry.isDirectory()||entry.getName().equals("..\\"))
continue;
BufferedInputStream bin = new BufferedInputStream(zin);
byte[] buf = new byte[];
bin.read(buf,0,1);
}

⑽ 如何使用Zlib解壓內存塊中的文件

1 准備工作。 下載zlib.dll。以及相關頭文件。將dll文件及頭文件加入工程。 2 壓縮: 調用函數compress. 形式為 int compress(Byte * dest, uLong* destLen, const Byte *source, ULONG sourceLen); 功能是將source指向的空間,長度為sourceLen的數據進行壓縮,壓縮數據儲存在dest中,長度由參數destLen返回。 如果壓縮出錯,返回對應錯誤號,否則返回0. 3解壓縮: 調用函數uncompress. 形式為 int uncompress(Byte * dest, uLong* destLen, const Byte *source, ULONG sourceLen); 功能是將source指向的空間,長度為sourceLen的數據進行解壓縮,解壓縮後的數據儲存在dest中,長度由參數destLen返回。 如果解壓縮出錯,返回對應錯誤號,否則返回0.

閱讀全文

與czlib解壓縮相關的資料

熱點內容
卡爾曼濾波演算法書籍 瀏覽:769
安卓手機怎麼用愛思助手傳文件進蘋果手機上 瀏覽:844
安卓怎麼下載60秒生存 瀏覽:803
外向式文件夾 瀏覽:240
dospdf 瀏覽:431
怎麼修改騰訊雲伺服器ip 瀏覽:392
pdftoeps 瀏覽:496
為什麼鴻蒙那麼像安卓 瀏覽:736
安卓手機怎麼拍自媒體視頻 瀏覽:186
單片機各個中斷的初始化 瀏覽:724
python怎麼集合元素 瀏覽:481
python逐條解讀 瀏覽:833
基於單片機的濕度控制 瀏覽:499
ios如何使用安卓的帳號 瀏覽:883
程序員公園采訪 瀏覽:812
程序員實戰教程要多長時間 瀏覽:979
企業數據加密技巧 瀏覽:135
租雲伺服器開發 瀏覽:814
程序員告白媽媽不同意 瀏覽:337
攻城掠地怎麼查看伺服器 瀏覽:601