导航:首页 > 文件处理 > java压缩流

java压缩流

发布时间:2022-01-20 10:27:46

java中zip压缩输入输出流的问题,ZipEntry()方法里的参数到底什么意思

ZipEntry 用于保存一些被压缩文件的信息,如文件名,最后访问时间,最后修改时间,创建时间,文件大小,crc 校验值 等信息。

ZipEntry 具有一个带 String 类型参数的构造方法:ZipEntry(String name), name 是入口名称,就是打开压缩文件时,看到的里面的文件名称。

可以看一下它的源码,下面是部分源码:

public
,Cloneable{

Stringname;//entryname
longtime=-1;//lastmodificationtime
FileTimemtime;//lastmodificationtime,fromextrafielddata
FileTimeatime;//lastaccesstime,fromextrafielddata
FileTimectime;//creationtime,fromextrafielddata
longcrc=-1;//crc-32ofentrydata
longsize=-1;//uncompressedsizeofentrydata
longcsize=-1;//compressedsizeofentrydata
intmethod=-1;//compressionmethod
intflag=0;//generalpurposeflag
byte[]extra;//
Stringcomment;//optionalcommentstringforentry

...
}

② 如何用java读到正在压缩的压缩包(使用zip的不压缩模式)的流

你要知道对应压缩文件的解码方式,所谓打开压缩文件,就是相当于解压缩,然后放到一个地方
你如果能自己实现对应的压缩方法,很容易办到

③ java 传递压缩文件用什么流

你可以用GZIPInputStream和GZIPOutputStream ,这是专门用来读写压缩流的

④ 你好,最近我也遇到用java压缩和解压,向你咨询下你的解决方案什么,你是怎么用文件流方式去压缩

package com.onewaveinc.cwds.commons.utils;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Enumeration;
import org.apache.tools.zip.ZipEntry;
import org.apache.tools.zip.ZipFile;
import org.apache.tools.zip.ZipOutputStream;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* @author fz 2010-7-30
* @Description 把指定文件夹下的所有文件压缩为指定文件夹下指定zip文件;把指定文件夹下的zip文件解压到指定目录下
*/
public class ZipUtils {

private static final Logger logger = LoggerFactory.getLogger(ZipUtils.class);

private static final String SEPARATE = "/";

/**
* @Author fz 2010-7-30
* @param sourceDir 待压缩目录
* @param zipFile 压缩文件名称
* @throws Exception
* @Description 把sourceDir目录下的所有文件进行zip格式的压缩,保存为指定zip文件
*/
public static void zip(String sourceDir, String zipFile) throws Exception {
OutputStream os = null;
// try {
os = new FileOutputStream(zipFile);
BufferedOutputStream bos = new BufferedOutputStream(os);
ZipOutputStream zos = new ZipOutputStream(bos);
File file = new File(sourceDir);
String basePath = null;

if (file.isDirectory()) {
basePath = file.getPath();
} else {
// 直接压缩单个文件时,取父目录
basePath = file.getParent();
}
zipFile(file, basePath, zos);
zos.closeEntry();
zos.close();
// } catch (Exception e) {
// logger.error("压缩文件或文件夹" + sourceDir + "时发生异常");
// e.printStackTrace();
// }

}

/**
* @Author fz 2010-7-30
* @param source 源文件
* @param basePath 待压缩文件根目录
* @param zos 文件压缩流
* @Description 执行文件压缩成zip文件
*/
private static void zipFile(File source, String basePath, ZipOutputStream zos) {
File[] files = new File[0];
if (source.isDirectory()) {
files = source.listFiles();
} else {
files = new File[1];
files[0] = source;
}

//存相对路径(相对于待压缩的根目录)
String pathName = null;
byte[] buf = new byte[1024];
int length = 0;
try {
for (File file : files) {
if (file.isDirectory()) {
pathName = file.getPath().substring(basePath.length() + 1) + SEPARATE;
zos.putNextEntry(new ZipEntry(pathName));
zipFile(file, basePath, zos);
} else {
pathName = file.getPath().substring(basePath.length() + 1);
InputStream is = new FileInputStream(file);
BufferedInputStream bis = new BufferedInputStream(is);
zos.putNextEntry(new ZipEntry(pathName));
while ((length = bis.read(buf)) > 0) {
zos.write(buf, 0, length);
}
is.close();
}
}
} catch (Exception e) {
logger.error("压缩文件" + source + "时发生异常");
e.printStackTrace();
}
}

/**
* @Author fz 2010-7-30
* @param zipfile 待解压文件
* @param destDir 解压文件存储目录
* @throws Exception
* @Description 解压zip文件,只能解压zip文件
*/
@SuppressWarnings("unchecked")
public static void unZip(String zipfile, String destDir) throws Exception {
destDir = destDir.endsWith(SEPARATE) ? destDir : destDir + SEPARATE;
byte b[] = new byte[1024];
int length;
ZipFile zipFile;
// try {
zipFile = new ZipFile(new File(zipfile));
Enumeration enumeration = zipFile.getEntries();
ZipEntry zipEntry = null;
while (enumeration.hasMoreElements()) {
zipEntry = (ZipEntry) enumeration.nextElement();
File loadFile = new File(destDir + zipEntry.getName());
if (zipEntry.isDirectory()) {
loadFile.mkdirs();
} else {
if (!loadFile.getParentFile().exists()) {
loadFile.getParentFile().mkdirs();
}
OutputStream outputStream = new FileOutputStream(loadFile);
InputStream inputStream = zipFile.getInputStream(zipEntry);
while ((length = inputStream.read(b)) > 0)
outputStream.write(b, 0, length);
outputStream.close();
inputStream.close();
}
}
zipFile.close();
// } catch (IOException e) {
// logger.error("解压文件" + zipfile + "时发生异常");
// e.printStackTrace();
// }
}

}

⑤ java 压缩流 指定两个文件 压缩到一个压缩文件上,结果两个文件的内容 都写到了 第二个文件上

for (String string : list)
{
BufferedReader in=new BufferedReader(new FileReader(string));
int i=0;
zout.putNextEntry(new ZipEntry(string));
while((i=in.read())!=-1)
{
out.write(i);
}
out.flush();//------------------------------->强制输出缓冲区
in.close();
}
out.close();

⑥ java 以流的形式解压带密码的zip

可以使用 Runtime 直接调用 winRar 的命令行命令来解压缩
注意:
1、winRar命令使用,在dos下输入 unrar 就可以看到全部的命令说明。该命令在winRar的安装目录下
2、winRar命令行命令的路径问题,也就是path。要么加入系统变量path中,要么在winRar的安装目录下执行程序
以下是程序代码,解压 test.rar 到当前目录下,密码123

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.InputStreamReader;

public class TestRunTime {

public static void main(String[] args) {
Runtime run = Runtime.getRuntime();
try {
Process p = run.exec("unrar e test.rar -p123");//执行解压缩命令
BufferedInputStream in = new BufferedInputStream(p.getInputStream());
BufferedReader inBr = new BufferedReader(new InputStreamReader(in));
String lineStr;

while ((lineStr = inBr.readLine()) != null)
System.out.println(lineStr);
// 检查命令是否执行失败。
if (p.waitFor() != 0) {
if (p.exitValue() == 1)// p.exitValue()==0表示正常结束,1:非正常结束
System.err.println("命令执行失败!");
}

} catch (Exception e) {
e.printStackTrace();
}

}

}

⑦ java Zip压缩输入输出流问题

带目录结构的压缩,以方便解压时得到原目录结构来存放。

⑧ java DeflaterInputStream 压缩 输入流

有一个你就读取一个,循环读取就行了。

⑨ Java中压缩输出流的问题

http://blog.csdn.net/hanshileiai/article/details/6718375
这里刚好有个例子

⑩ java后台怎么接收一个gzip压缩流,并且解析接受参数

原则上,不需要在代码中处理zip只接收就可以。解析可以按HTTP协议自己解析,也可以使用WEB容器完成

阅读全文

与java压缩流相关的资料

热点内容
程序员需不需要眼镜 浏览:545
经济学pdf下载 浏览:748
程序员拿到offer复工 浏览:431
镜像解压一半自动断电怎么回事 浏览:985
程序员农村别墅价格 浏览:395
梦幻西游网页版通用服务器是什么 浏览:330
天天爱消除算法 浏览:778
隐含模块编译 浏览:482
c语言寻路算法 浏览:262
图片背景分割python 浏览:537
程序员大数据专业有前途吗 浏览:428
特征融合算法 浏览:403
安卓如何显示关闭状态 浏览:309
手机版云文档文件夹怎么添加文件 浏览:359
安卓版侠盗猎车怎么招小弟 浏览:763
官方小程序示例源码 浏览:140
程序员努力视频 浏览:683
程序员的爱情是什么样的 浏览:633
单片机太阳能追踪器 浏览:853
技术指标编程选股公式 浏览:979