当前位置:首页 » 文件管理 » 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;

热点内容
我的世界ec服务器消失了 发布:2024-10-07 21:48:30 浏览:661
pythonziptodict 发布:2024-10-07 21:36:09 浏览:790
linux操作系统教程 发布:2024-10-07 21:16:54 浏览:280
摩托音响怎么配置 发布:2024-10-07 21:12:37 浏览:348
本田思域哪个配置好 发布:2024-10-07 21:04:11 浏览:347
算法期末试卷 发布:2024-10-07 20:51:04 浏览:808
编译期错误提示 发布:2024-10-07 20:07:23 浏览:297
阿里云服务器打开慢 发布:2024-10-07 20:06:33 浏览:578
磁存储行业的前景 发布:2024-10-07 20:01:29 浏览:549
android对象序列化 发布:2024-10-07 20:01:28 浏览:760