http://blog.csdn.net/hbhgjiangkun/article/details/7282629?reload#comments
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.List;
import org.apache.tools.zip.ZipEntry;
import org.apache.tools.zip.ZipOutputStream;
/**
* 根据文件名称压缩文件
* @author
* @version 1.0
* 20140804 14:51
*
*/
public class FileUtil {
//文件打包下载
public static String downLoadFiles()
throws Exception {
ZipOutputStream zipOut=null;
FileOutputStream fous=null;
try {
/**这个集合就是你想要打包的所有文件*/
List<File> files = new ArrayList<File>();
//压缩的文件url
// String[] fileUrl=batchFileUrl.split(",");
files.add(new File("f:/test/download/dw1.doc"));
files.add(new File("f:/test/download/dw2.doc"));
files.add(new File("f:/test/download/测试地址及密码.doc"));
// for(int k=0;k<fileUrl.length;k++){
// //String src=DownloadFileUtil.convert(context.getRealPath("/temp")+"/"+fileUrl[k], "gb2312", "utf-8");
// files.add(new File(context.getRealPath("/temp")+"/"+fileUrl[k]));
// }
/**创建一个临时压缩文件,
* 我们会把文件流全部注入到这个文件中
* 这里的文件你可以自定义是.rar还是.zip*/
String zipName=System.currentTimeMillis()+".zip";
//压缩文件保存地址
// String zipUrl=context.getRealPath("/downloadZip/zip")+zipName;
String zipUrl="f:/test/download/"+zipName;
File file = new File(zipUrl);
if (!file.exists()){
file.createNewFile();
}
// response.reset();
//创建文件输出流
fous = new FileOutputStream(file);
/**打包的方法我们会用到ZipOutputStream这样一个输出流,
* 所以这里我们把输出流转换一下*/
/**
* 一定要用BufferedOutputStream 转换一下
* 要不就会出现这样的错误:
* java.io.IOException: Write error
at java.io.FileOutputStream.writeBytes(Native Method)
at java.io.FileOutputStream.write(FileOutputStream.java:260)
at org.apache.tools.zip.ZipOutputStream.writeOut(ZipOutputStream.java:881)
at org.apache.tools.zip.ZipOutputStream.writeOut(ZipOutputStream.java:864)
at org.apache.tools.zip.ZipOutputStream.writeCentralDirectoryEnd(ZipOutputStream.java:773)
at org.apache.tools.zip.ZipOutputStream.finish(ZipOutputStream.java:318)
at org.apache.tools.zip.ZipOutputStream.close(ZipOutputStream.java:515)
at com.zyz.FileUtil.downLoadFiles(FileUtil.java:70)
at com.zyz.FileUtil.main(FileUtil.java:132)
*/
zipOut=new ZipOutputStream(new BufferedOutputStream(fous));
/**这个方法接受的就是一个所要打包文件的集合,
* 还有一个ZipOutputStream*/
zipFile(files, zipOut);
zipOut.close();
fous.close();
//返回压缩文件路径
return zipUrl;
}catch (Exception e) {
e.printStackTrace();
}finally{
if(zipOut!=null){
zipOut.close();
}
if(fous !=null){
fous.close();
}
}
return null;
}
/**
* 把接受的全部文件打成压缩包
* @param List<File>;
* @param org.apache.tools.zip.ZipOutputStream
*/
public static void zipFile(List<File> files,ZipOutputStream outputStream) {
int size = files.size();
for(int i = 0; i < size; i++) {
File file = (File) files.get(i);
zipFile(file, outputStream);
}
}
/**
* 根据输入的文件与输出流对文件进行打包
* @param File
* @param org.apache.tools.zip.ZipOutputStream
*/
public static void zipFile(File inputFile,ZipOutputStream ouputStream) {
try {
if(inputFile.exists()) {
/**如果是目录的话这里是不采取操作的*/
if (inputFile.isFile()) {
FileInputStream IN = new FileInputStream(inputFile);
BufferedInputStream bins = new BufferedInputStream(IN, 512);
//org.apache.tools.zip.ZipEntry
ZipEntry entry = new ZipEntry(inputFile.getName());
ouputStream.putNextEntry(entry);
// 向压缩文件中输出数据
int nNumber;
byte[] buffer = new byte[512];
while ((nNumber = bins.read(buffer)) != -1) {
ouputStream.write(buffer, 0, nNumber);
}
// 关闭创建的流对象
bins.close();
IN.close();
} else {
try {
File[] files = inputFile.listFiles();
for (int i = 0; i < files.length; i++) {
zipFile(files[i], ouputStream);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
try {
downLoadFiles();
System.out.println("执行完:");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}