當前位置:首頁 » 文件管理 » zlib解壓文件

zlib解壓文件

發布時間: 2022-10-16 17:01:57

A. java 如何用zlib解壓縮tar.gz文件

public static void makeZip(List<File> fileList,String zipPath,boolean isDelete) {
byte[] buf = new byte[1024];
try {
// Create the ZIP file
File zipFile = new File(zipPath);
ZipOutputStream out = new ZipOutputStream(new FileOutputStream(zipFile));
// Compress the files
for (int i = 0; i < fileList.size(); i++) {
FileInputStream in = new FileInputStream(fileList.get(i));
// Add ZIP entry to output stream.
out.putNextEntry(new ZipEntry(fileList.get(i).getName()));
// Transfer bytes from the file to the ZIP file
int len;
while ( (len = in.read(buf)) > 0) {
out.write(buf, 0, len);
}
// Complete the entry
out.closeEntry();
in.close();
}
// Complete the ZIP file
out.close();
System.out.println("壓縮完成.");

//把舊的文件刪除
if(isDelete == true){
for (int i = 0; i < fileList.size(); i++) {
File oldFile = fileList.get(i);
oldFile.delete();
}
}
}
catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args){
File in1=new File("D:\\a.txt");
File in2=new File("D:\\b.txt");
File[] file=new File[]{in1,in2};
File zip=new File("D:\\ab.zip");
IDMZip mgr=new IDMZip();
mgr.ZipFiles(file, zip);
}

這個方法不管你是在windows下還是在linux下,都能正常執行。

B. 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;

C. 在Linux下,用zlib寫解壓文件的C程序,需要事先知道文件壓縮前的大小么怎麼得到

.gz文件的最後4位元組就是壓縮前的原長度(ISIZE),並且倒數第二個4位元組是壓縮前原buffer的CRC32冗餘校驗值。參見標准文檔 rfc1952 (https://tools.ietf.org/html/rfc1952).

D. 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.

E. 為什麼nodejs 使用zlib解壓文件出現錯誤

安裝nodeJs流程:1、下載nodejs引擎,32bitversion或者64bitversion2、下載最新版的npm zip格式壓縮包:3、在硬碟某個位置,如D盤下建立一個文件nodejs,把上面兩個下載的東西都放在這里,npm要解壓。4、配置兩個環境變數:一個是PATH上增加node.exe的目錄D:\\nodejs,一個是增加環境變數NODE_PATH,值為D:\\nodejs\\node_moles。5、win7環境配置在系統》高級系統設置》高級》環境變數》系統變數中查找PATH,編輯加上D:\\nodejs,再加上NODE_PATH變數和值。6、安裝express:打開cmd命令行,使用命令行定位到這Node目錄下,鍵入指令npminstallexpress[安裝express至相對路徑]或npminstallexpress-g[安裝express至絕對路徑]到這里,你在命令行裡面輸入node-v如果輸出nodejs的版本則安裝成功。更新nodejs的版本可以在命令行中輸入: npmupdatenpm-g

F. nodejs zlib 怎麼把幾個壓縮過的文件解壓拼接

我請求管用所結束httpvar http = require("http"),
zlib = require("zlib");

function getGzipped(url, callback) {
// buffer to store the streamed decompression
var buffer = [];

http.get(url, function(res) {
// pipe the response into the gunzip to decompress
var gunzip = zlib.createGunzip();
res.pipe(gunzip);

gunzip.on('data', function(data) {
// decompression chunk ready, add it to the buffer
buffer.push(data.toString())

}).on("end", function() {
// response and decompression complete, join the buffer and return
callback(null, buffer.join(""));

}).on("error", function(e) {
callback(e);
})
}).on('error', function(e) {
callback(e)
});
}

getGzipped(url, function(err, data) {
console.log(data);
});

2. 嘗試添加encoding: null給傳遞給選項request避免載體轉換字元串並保持二進制緩沖區

3. 工作示例(使用節點請求模塊)gunzips響應function gunzipJSON(response){

var gunzip = zlib.createGunzip();
var json = "";

gunzip.on('data', function(data){
json += data.toString();
});

gunzip.on('end', function(){
parseJSON(json);
});

response.pipe(gunzip);
}

全碼:

4. 像@Iftah說設置encoding: null 完整例(少錯誤處理):request = require('request');
zlib = require('zlib');

request(url, {encoding: null}, function(err, response, body){

if(response.headers['content-encoding'] == 'gzip'){

zlib.gunzip(body, function(err, dezipped) {
callback(dezipped.toString());
}

} else {
callback(body);
}
});

G. zlib下載文件在哪

第一步 下載並解壓zlib壓縮包

打開zlib官網,找到下載鏈接,右鍵復制地址:

在Linux中使用wget命令下載,執行如下命令開始下載:

wget http://zlib.net/zlib-1.2.8.tar.gz

解壓:

tar zxvf zlib-1.2.8.tar.gz

第二步 開始安裝

安裝過程比較簡單,進入zlib的解壓目錄,依次執行下面幾條命令即可:

配置:

./configure

如果之前沒有安裝gcc(C 編譯器),這一步將報如下錯誤信息::

xueliang@dev:~/download/zlib-1.2.8$ ./configure

Checking for gcc…

Compiler error reporting is too harsh for ./configure (perhaps remove -Werror).

** ./configure aborting.

xueliang@dev:~/download/zlib-1.2.8$

希望我的回答能對你有所幫助。

H. 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;
}

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

熱點內容
華為電腦伺服器系統進不去提示 發布:2024-10-08 00:13:42 瀏覽:490
登錄密碼如何獲取 發布:2024-10-07 23:58:40 瀏覽:424
王者榮耀人機腳本 發布:2024-10-07 23:58:33 瀏覽:807
地標建築腳本 發布:2024-10-07 23:48:51 瀏覽:242
sqlorderby 發布:2024-10-07 23:42:13 瀏覽:958
qq秒源碼 發布:2024-10-07 23:41:37 瀏覽:471
51單片機編譯器 發布:2024-10-07 23:28:04 瀏覽:798
安卓手機陌陌封設備了怎麼搞 發布:2024-10-07 23:17:00 瀏覽:180
sql管理系統代碼 發布:2024-10-07 23:00:51 瀏覽:524
安卓什麼瀏覽器可以打開 發布:2024-10-07 23:00:51 瀏覽:121