利用JAVA打包生成zip文件的类
来源:互联网 发布:依稀网络语是什么意思 编辑:程序博客网 时间:2024/05/16 18:40
主要代码参考了beansoft的ZipUtil,添加了一个方法,
可以把不同目录文件添加到zip中,并做了一些改进,使用了apache的zip类,(原为jdk的zip类),使支持中文文件名。
apache的zip类可以从ant中抽取。apache好像没有提供单独下载。
package com.order;
import java.io.*;
import java.util.List;
import org.apache.tools.zip.*;
/**
* 把多个文件打包到一个文件
* @author xiaofei.hu
*
*/
public class ZipUtil {
public static void toZip(File[] files, File zipFile){
}
/**
* The buffer.
*/
protected static byte buf[] = new byte[1024];
/**
* 遍历目录并添加文件.
* @param jos - JAR 输出流
* @param file - 目录文件名
* @param pathName - ZIP中的目录名
* @throws IOException
* @throws FileNotFoundException
*/
private static void recurseFiles(ZipOutputStream jos, File file, String pathName)
throws IOException, FileNotFoundException
{
if (file.isDirectory())
{
pathName = pathName + file.getName() + "/";
jos.putNextEntry(new ZipEntry(pathName));
String fileNames[] = file.list();
if (fileNames != null)
{
for (int i = 0; i < fileNames.length; i++)
recurseFiles(jos, new File(file, fileNames[i]), pathName);
}
} else
{
ZipEntry jarEntry = new ZipEntry(pathName + file.getName());
// System.out.println(pathName + " " + file.getName());
FileInputStream fin = new FileInputStream(file);
BufferedInputStream in = new BufferedInputStream(fin);
jos.putNextEntry(jarEntry);
int len;
while ((len = in.read(buf)) >= 0)
jos.write(buf, 0, len);
in.close();
jos.closeEntry();
}
}
public static void toZip(List<File> files, File zipFile, String zipFolderName, int level)
throws IOException, FileNotFoundException
{
level = checkZipLevel(level);
if(zipFolderName == null) {
zipFolderName = "";
}
ZipOutputStream jos = new ZipOutputStream(new FileOutputStream(zipFile));
jos.setLevel(level);
for (int i = 0; i < files.size(); i++){
recurseFiles(jos, files.get(i), files.get(i).getPath());
}
jos.close();
}
/**
* 创建 ZIP/JAR 文件.
* @param directory - 要添加的目录
* @param zipFile - 保存的 ZIP 文件名
* @param zipFolderName - ZIP 中的路径名
* @param level - 压缩级别(0~9)
* @throws IOException
* @throws FileNotFoundException
*/
public static void makeDirectoryToZip(File directory, File zipFile, String zipFolderName, int level)
throws IOException, FileNotFoundException
{
level = checkZipLevel(level);
if(zipFolderName == null) {
zipFolderName = "";
}
ZipOutputStream jos = new ZipOutputStream(new FileOutputStream(zipFile));
jos.setLevel(level);
String fileNames[] = directory.list();
if (fileNames != null)
{
for (int i = 0; i < fileNames.length; i++)
recurseFiles(jos, new File(directory, fileNames[i]), zipFolderName);
}
jos.close();
}
/**
* 检查并设置有效的压缩级别.
* @param level - 压缩级别
* @return 有效的压缩级别或者默认压缩级别
*/
public static int checkZipLevel(int level)
{
if(level < 0 || level > 9) level = 7;
return level;
}
}
文章来源:http://huxiaofei590.blog.163.com/blog/static/3259612200711457331
- 利用JAVA打包生成zip文件的类
- java打包文件生成zip压缩包
- java 打包zip文件
- java 打包Zip文件
- JAVA文件打包ZIP
- java生成zip文件打包操作代码(包括解压缩)
- Java生成ZIP文件
- java 生成Zip文件
- 下载文件打包 zip java
- 使用JAVA生成zip文件的方法:
- java 实现多个文件打包成zip的功能
- java将文件打包成zip文件
- php生成压缩zip文件的类
- 怎样用Java生成ZIP文件
- java写的将文件或文件夹打包成zip的工具类
- java将文件打包成ZIP压缩文件的工具类实例
- java将文件打包成ZIP压缩文件的工具类实例
- java将文件打包成ZIP压缩文件的工具类实例
- Vs008中关于托盘气泡提示显示不出来的问题and sdk实现托盘加气泡
- Linux信号简介和信号处理相关函数
- 设计模式学习总结
- JasperReport 与 Struts2 整合
- 如何找回删除并清空回收站的东西
- 利用JAVA打包生成zip文件的类
- 硬件瓶颈和软件瓶颈
- get()和getline()
- 关于Apple和Google——转
- linux捕获KILL命令的信号
- C语言深度解剖 学习笔记
- MyEclipse整合SSH模式
- 做seo内容题目很重要
- struts2关于checkbox处理的一个bug