java 如何将多个文件打包成一个zip ?

sking_2008 2009-07-02 03:59:05

我可以得到需要下载的文件路径
String[] path;//这个已知
就是如何将这些文件打包成一个zip供下载?
希望高手们写下详细代码.``
...全文
3482 23 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
23 条回复
切换为时间正序
请发表友善的回复…
发表回复
nchzh302 2010-06-02
  • 打赏
  • 举报
回复
学习下
yinliao 2010-05-27
  • 打赏
  • 举报
回复
要用到了。。。。学习一下。。
myhonor 2009-07-13
  • 打赏
  • 举报
回复
貌似JDK自带的那个ZIP有些问题,网上都建议用APACHE的
Looking_Glass 2009-07-11
  • 打赏
  • 举报
回复
日!这个问题我已经发过N次拉!
caironghuicxz 2009-07-10
  • 打赏
  • 举报
回复
我看已经解决了这个问题了啊。
cuij7718 2009-07-10
  • 打赏
  • 举报
回复
不管如何做,如果用JDK 的zip压缩,中文问题早晚会碰到,如果碰到了,到apache上面下在他的zip包用就可以了,这个不是单独的一个zip,是和别的打包在一起的,哪一个忘了
找到以后删除多余的,最终也就70kb左右,支持zip,tar,gzip
老张-AI 2009-07-10
  • 打赏
  • 举报
回复
关注
网络科技 2009-07-10
  • 打赏
  • 举报
回复
要用第三方包,学习了
cuij7718 2009-07-10
  • 打赏
  • 举报
回复
真是重赏之下必有勇夫,还真有给写代码的,给一个思路就可以了
薪水 2009-07-06
  • 打赏
  • 举报
回复
帮顶
lcb521 2009-07-06
  • 打赏
  • 举报
回复
关注!
pathuang68 2009-07-06
  • 打赏
  • 举报
回复
飘过,学习
Monkey_D_Luffy 2009-07-06
  • 打赏
  • 举报
回复
看起来已经解决了
ttmotor2008 2009-07-04
  • 打赏
  • 举报
回复
路过,学习.
hbwhwang 2009-07-03
  • 打赏
  • 举报
回复
代码运行需要 ant.jar


import java.io.*;
import java.util.Enumeration;

import org.apache.tools.zip.ZipEntry;
import org.apache.tools.zip.ZipFile;
import org.apache.tools.zip.ZipOutputStream;

public class ZipUtil {

/**
* 压缩。
*
* @param src
* 源文件或者目录
* @param dest
* 压缩文件路径
* @throws IOException
*/
public static void zip(String src, String dest) throws IOException {
ZipOutputStream out = null;
try {
File outFile = new File(dest);
out = new ZipOutputStream(outFile);
File fileOrDirectory = new File(src);

if (fileOrDirectory.isFile()) {
zipFileOrDirectory(out, fileOrDirectory, "");
} else {
File[] entries = fileOrDirectory.listFiles();
for (int i = 0; i < entries.length; i++) {
// 递归压缩,更新curPaths
zipFileOrDirectory(out, entries[i], "");
}
}

} catch (IOException ex) {
ex.printStackTrace();
} finally {
if (out != null) {
try {
out.close();
} catch (IOException ex) {
}
}
}
}

/**
* 递归压缩文件或目录
*
* @param out
* 压缩输出流对象
* @param fileOrDirectory
* 要压缩的文件或目录对象
* @param curPath
* 当前压缩条目的路径,用于指定条目名称的前缀
* @throws IOException
*/
private static void zipFileOrDirectory(ZipOutputStream out,
File fileOrDirectory, String curPath) throws IOException {
FileInputStream in = null;
try {
if (!fileOrDirectory.isDirectory()) {
// 压缩文件
byte[] buffer = new byte[4096];
int bytes_read;
in = new FileInputStream(fileOrDirectory);

ZipEntry entry = new ZipEntry(curPath
+ fileOrDirectory.getName());
out.putNextEntry(entry);

while ((bytes_read = in.read(buffer)) != -1) {
out.write(buffer, 0, bytes_read);
}
out.closeEntry();
} else {
// 压缩目录
File[] entries = fileOrDirectory.listFiles();
for (int i = 0; i < entries.length; i++) {
// 递归压缩,更新curPaths
zipFileOrDirectory(out, entries[i], curPath
+ fileOrDirectory.getName() + "/");
}
}
} catch (IOException ex) {
ex.printStackTrace();
throw ex;
} finally {
if (in != null) {
try {
in.close();
} catch (IOException ex) {
}
}
}
}

/**
* 解压缩。
*
* @param zipFileName
* 源文件
* @param outputDirectory
* 解压缩后文件存放的目录
* @throws IOException
*/
public static void unzip(String zipFileName, String outputDirectory)
throws IOException {

ZipFile zipFile = null;

try {
zipFile = new ZipFile(zipFileName);
Enumeration e = zipFile.getEntries();

ZipEntry zipEntry = null;

File dest = new File(outputDirectory);
dest.mkdirs();

while (e.hasMoreElements()) {
zipEntry = (ZipEntry) e.nextElement();

String entryName = zipEntry.getName();

InputStream in = null;
FileOutputStream out = null;

try {
if (zipEntry.isDirectory()) {
String name = zipEntry.getName();
name = name.substring(0, name.length() - 1);

File f = new File(outputDirectory + File.separator
+ name);
f.mkdirs();
} else {
int index = entryName.lastIndexOf("\\");
if (index != -1) {
File df = new File(outputDirectory + File.separator
+ entryName.substring(0, index));
df.mkdirs();
}
index = entryName.lastIndexOf("/");
if (index != -1) {
File df = new File(outputDirectory + File.separator
+ entryName.substring(0, index));
df.mkdirs();
}

File f = new File(outputDirectory + File.separator
+ zipEntry.getName());
// f.createNewFile();
in = zipFile.getInputStream(zipEntry);
out = new FileOutputStream(f);

int c;
byte[] by = new byte[1024];

while ((c = in.read(by)) != -1) {
out.write(by, 0, c);
}
out.flush();
}
} catch (IOException ex) {
ex.printStackTrace();
throw new IOException("解压失败:" + ex.toString());
} finally {
if (in != null) {
try {
in.close();
} catch (IOException ex) {
}
}
if (out != null) {
try {
out.close();
} catch (IOException ex) {
}
}
}
}

} catch (IOException ex) {
ex.printStackTrace();
throw new IOException("解压失败:" + ex.toString());
} finally {
if (zipFile != null) {
try {
zipFile.close();
} catch (IOException ex) {
}
}
}

}

public static void main(String[] args) {
try {
ZipUtil.zip("C:\\0115080120081024","d:\\0115080120081027.zip");
//ZipUtil.unzip("D:\\a\\010108012008-8-25.zip", "d:\\a\\tmp");
} catch (Exception e) {
e.printStackTrace();
}

}

}


neohope 2009-07-02
  • 打赏
  • 举报
回复
打成jar就不行吗?将源代码一起打进去
晨星 2009-07-02
  • 打赏
  • 举报
回复
如果可以的话,调用ant任务更简单。嘿嘿。
scueczhang 2009-07-02
  • 打赏
  • 举报
回复
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import org.apache.tools.zip.ZipEntry;
import org.apache.tools.zip.ZipOutputStream;

public class DirectoryZip {
public static void jar(String inputFileName, String outputFileName) throws Exception {
ZipOutputStream out = new ZipOutputStream(new FileOutputStream(outputFileName));
out.setEncoding("GBK"); // ###### 这句话是关键,指定输出的编码方式
File f = new File(inputFileName);
jar(out, f, "");
out.close();
}

private static void jar(ZipOutputStream out, File f, String base) throws Exception {
if (f.isDirectory()) {
File[] fl = f.listFiles();
base = base.length() == 0 ? "" : base + "/";
out.putNextEntry(new ZipEntry(base));
for (int i = 0; i < fl.length; i++) {
jar(out, fl[i], base + fl[i].getName());
}
} else {
out.putNextEntry(new ZipEntry(base));
FileInputStream in = new FileInputStream(f);
byte[] buffer = new byte[1024];
int n = in.read(buffer);
while (n != -1) {
out.write(buffer, 0, n);
n = in.read(buffer);
}
in.close();
}
}

public static void main(String[] args) {
try {
jar("D:\\temp\\1", "d://mytest.zip");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
yanhan0615 2009-07-02
  • 打赏
  • 举报
回复
网上有现成的,直接递归即可
老紫竹 2009-07-02
  • 打赏
  • 举报
回复
整个目录的压缩成一个zip文件

其实就是一个不断的压缩的过程!
加载更多回复(1)

62,635

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧