java语言 ant.jar zip打包 utils工具类 通用性强
2014-01-05 20:37
357 查看
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.tools.zip.ZipEntry;
import org.apache.tools.zip.ZipOutputStream;
public class ZipUtil {
public static void ZipFiles(List<String> srcfile, String zipPath) {
byte[] buf = new byte[4096];
ZipOutputStream out = null;
try {
// 创建zip输出流
out = new ZipOutputStream(new FileOutputStream(zipPath));
// 循环将源文件列表添加到zip文件中
for (int i = 0; i < srcfile.size(); i++) {
File file = new File(srcfile.get(i));
FileInputStream in = new FileInputStream(file);
String fileName = file.getName();
out.putNextEntry(new ZipEntry(fileName));
int len;
while ( (len = in.read(buf)) > 0) {
out.write(buf, 0, len);
}
out.closeEntry();
in.close();
}
}
catch (IOException e) {
e.printStackTrace();
} finally {
if (null != out) {
try {
out.close();
out = null;
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
public static void ZipFiles(HttpServletRequest request, HttpServletResponse response, List<String> srcfile, String downloadZipFileName) {
byte[] buf = new byte[4096];
try {
// Create the ZIP file
// ZipOutputStream out = new ZipOutputStream(new FileOutputStream(zipPath));
ZipOutputStream out = new ZipOutputStream(response.getOutputStream());//--设置成这样可以不用保存在本地,再输出, 通过response流输出。
// Compress the files
if (request.getHeader("User-Agent").toLowerCase().indexOf("firefox") > 0) {
downloadZipFileName = new String(downloadZipFileName.getBytes("GB2312"),"ISO-8859-1");
} else {
// 对文件名进行编码处理中文问题
downloadZipFileName = java.net.URLEncoder.encode(downloadZipFileName, "UTF-8");// 处理中文文件名的问题
downloadZipFileName = new String(downloadZipFileName.getBytes("UTF-8"), "GBK");// 处理中文文件名的问题
}
response.reset(); // 重点突出
response.setCharacterEncoding("UTF-8"); // 重点突出
response.setContentType("application/x-msdownload");// 不同类型的文件对应不同的MIME类型 // 重点突出
// inline在浏览器中直接显示,不提示用户下载
// attachment弹出对话框,提示用户进行下载保存本地
// 默认为inline方式
response.setHeader("Content-Disposition", "attachment;filename="+downloadZipFileName);
for (int i = 0; i < srcfile.size(); i++) {
File file = new File(srcfile.get(i));
FileInputStream in = new FileInputStream(file);
// Add ZIP entry to output stream.
String fileName = file.getName();
out.putNextEntry(new ZipEntry(fileName));
// 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("压缩完成.");
}
catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
List<String> paths = new ArrayList<String>();
paths.add("d:/地理编码系统详细设计V1.0.docx");
paths.add("d:/中国联通协同办公项目.ppt");
ZipFiles(paths, "d:/zipfiles/info.zip");
}
}
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.tools.zip.ZipEntry;
import org.apache.tools.zip.ZipOutputStream;
public class ZipUtil {
public static void ZipFiles(List<String> srcfile, String zipPath) {
byte[] buf = new byte[4096];
ZipOutputStream out = null;
try {
// 创建zip输出流
out = new ZipOutputStream(new FileOutputStream(zipPath));
// 循环将源文件列表添加到zip文件中
for (int i = 0; i < srcfile.size(); i++) {
File file = new File(srcfile.get(i));
FileInputStream in = new FileInputStream(file);
String fileName = file.getName();
out.putNextEntry(new ZipEntry(fileName));
int len;
while ( (len = in.read(buf)) > 0) {
out.write(buf, 0, len);
}
out.closeEntry();
in.close();
}
}
catch (IOException e) {
e.printStackTrace();
} finally {
if (null != out) {
try {
out.close();
out = null;
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
public static void ZipFiles(HttpServletRequest request, HttpServletResponse response, List<String> srcfile, String downloadZipFileName) {
byte[] buf = new byte[4096];
try {
// Create the ZIP file
// ZipOutputStream out = new ZipOutputStream(new FileOutputStream(zipPath));
ZipOutputStream out = new ZipOutputStream(response.getOutputStream());//--设置成这样可以不用保存在本地,再输出, 通过response流输出。
// Compress the files
if (request.getHeader("User-Agent").toLowerCase().indexOf("firefox") > 0) {
downloadZipFileName = new String(downloadZipFileName.getBytes("GB2312"),"ISO-8859-1");
} else {
// 对文件名进行编码处理中文问题
downloadZipFileName = java.net.URLEncoder.encode(downloadZipFileName, "UTF-8");// 处理中文文件名的问题
downloadZipFileName = new String(downloadZipFileName.getBytes("UTF-8"), "GBK");// 处理中文文件名的问题
}
response.reset(); // 重点突出
response.setCharacterEncoding("UTF-8"); // 重点突出
response.setContentType("application/x-msdownload");// 不同类型的文件对应不同的MIME类型 // 重点突出
// inline在浏览器中直接显示,不提示用户下载
// attachment弹出对话框,提示用户进行下载保存本地
// 默认为inline方式
response.setHeader("Content-Disposition", "attachment;filename="+downloadZipFileName);
for (int i = 0; i < srcfile.size(); i++) {
File file = new File(srcfile.get(i));
FileInputStream in = new FileInputStream(file);
// Add ZIP entry to output stream.
String fileName = file.getName();
out.putNextEntry(new ZipEntry(fileName));
// 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("压缩完成.");
}
catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
List<String> paths = new ArrayList<String>();
paths.add("d:/地理编码系统详细设计V1.0.docx");
paths.add("d:/中国联通协同办公项目.ppt");
ZipFiles(paths, "d:/zipfiles/info.zip");
}
}
相关文章推荐
- java (jdk+ant.jar包)方式实现zip打包。
- eclipse通过maven建立java se工程配置log4j,打包成zip,将jar包和配置文件分开,并以bat和sh文件启动java程序
- 国际化语言工具类ResourceBundleUtils .java
- 普通java项目生成jar包的基本写法(利用ANT打包)
- 压缩工具类 - ZipUtils.java
- ZipUtils 工具类 - Java
- 用Ant 打包java工程为jar文件
- java 使用Apache ant.jar压缩解压zip--一种简单的实现
- java将文件打包成ZIP压缩文件的工具类实例
- Ant打包jar,参数名变为无意义的arg0,arg1,Java代码提示参数名称无意义
- 压缩工具类 - ZipUtils.java
- Java:Ant打包jar文件(有lib目录)
- 非java.util.zip,使用ant.jar制作zip压缩文件,以及相关中文解决方法
- java将文件打包成ZIP压缩文件的工具类实例
- java将文件打包成ZIP压缩文件的工具类实例
- 【Ant】Ant实例(1)一个Java文件依赖于某jar包,需要打包部署
- Java中如何将包中工具类进行打包导出做成jar文件
- 用ant将java程序打包成jar
- java将文件打包成ZIP压缩文件的工具类实例
- java将文件打包成ZIP压缩文件的工具类实例