ZipOutputStream out=new ZipOutputStream(new FileOutputStream(zipFileName))
for(int i=0i<fileList.size()i++){
String filename = (String)fileList.get(i)
File file = new File(filename)
zip(out,file)
}
out.close()
import java.io.File
public class ZipCompressorByAnt {
private File zipFile
/**
* 压缩文件构造函数
* @param pathName 最终压缩生成的压缩文件:目录+压缩文件名.zip
*/
public ZipCompressorByAnt(String finalFile) {
zipFile = new File(finalFile)
}
/**
* 执行压缩操作
* @param srcPathName 需要被压缩的文件/文件夹
*/
public void compressExe(String srcPathName) {
System.out.println("srcPathName="+srcPathName)
File srcdir = new File(srcPathName)
if (!srcdir.exists()){
throw new RuntimeException(srcPathName + "不存在!")
}
Project prj = new Project()
Zip zip = new Zip()
zip.setProject(prj)
zip.setDestFile(zipFile)
FileSet fileSet = new FileSet()
fileSet.setProject(prj)
fileSet.setDir(srcdir)
//fileSet.setIncludes("**/*.java")//包括哪些文件或文件夹 eg:zip.setIncludes("*.java")
//fileSet.setExcludes(...)//排除哪些文件或文件夹
zip.addFileset(fileSet)
zip.execute()
}
}
public class TestZip {
public static void main(String[] args) {
ZipCompressorByAnt zca = new ZipCompressorByAnt("E:\\test1.zip ")
zca.compressExe("E:\\test1")
}
}
/*如果 出现ant 的 52 51 50 等版本问题 可以去找对应的ant-1.8.2.jar 我开始用的ant-1.10.1.jar 就是这个包版本高了 一直报verson 52 版本问题*/
import java.io.BufferedOutputStream
import java.io.File
import java.io.FileInputStream
import java.io.FileOutputStream
import org.apache.tools.zip.ZipEntry
import org.apache.tools.zip.ZipOutputStream
/**
* @project: Test
* @author chenssy
* @date 2013-7-28
* @Description: 文件压缩工具类
* 将指定文件/文件夹压缩成zip、rar压缩文件
*/
public class CompressedFileUtil {
/**
* 默认构造函数
*/
public CompressedFileUtil(){
}
/**
* @desc 将源文件/文件夹生成指定格式的压缩文件,格式zip
* @param resourePath 源文件/文件夹
* @param targetPath 目的压缩文件保存路径
* @return void
* @throws Exception
*/
public void compressedFile(String resourcesPath,String targetPath) throws Exception{
File resourcesFile = new File(resourcesPath) //源文件
File targetFile = new File(targetPath) //目的
//如果目的路径不存在,则新建
if(!targetFile.exists()){
targetFile.mkdirs()
}
String targetName = resourcesFile.getName()+".zip" //目的压缩文件名
FileOutputStream outputStream = new FileOutputStream(targetPath+"\\"+targetName)
ZipOutputStream out = new ZipOutputStream(new BufferedOutputStream(outputStream))
createCompressedFile(out, resourcesFile, "")
out.close()
}
/**
* @desc 生成压缩文件。
* 如果是文件夹,则使用递归,进行文件遍历、压缩
* 如果是文件,直接压缩
* @param out 输出流
* @param file 目标文件
* @return void
* @throws Exception
*/
public void createCompressedFile(ZipOutputStream out,File file,String dir) throws Exception{
//如果当前的是文件夹,则进行进一步处理
if(file.isDirectory()){
//得到文件列表信息
File[] files = file.listFiles()
//将文件夹添加到下一级打包目录
out.putNextEntry(new ZipEntry(dir+"/"))
dir = dir.length() == 0 ? "" : dir +"/"
//循环将文件夹中的文件打包
for(int i = 0 i <files.length i++){
createCompressedFile(out, files[i], dir + files[i].getName()) //递归处理
}
}
else{ //当前的是文件,打包处理
//文件输入流
FileInputStream fis = new FileInputStream(file)
out.putNextEntry(new ZipEntry(dir))
//进行写操作
int j = 0
byte[] buffer = new byte[1024]
while((j = fis.read(buffer)) >0){
out.write(buffer,0,j)
}
//关闭输入流
fis.close()
}
}
public static void main(String[] args){
CompressedFileUtil compressedFileUtil = new CompressedFileUtil()
try {
compressedFileUtil.compressedFile("G:\\zip", "F:\\zip")
System.out.println("压缩文件已经生成...")
} catch (Exception e) {
System.out.println("压缩文件生成失败...")
e.printStackTrace()
}
}
}
运行程序结果如下:
压缩之前的文件目录结构:
如果是使用java.util下的java.util.zip进行打包处理,可能会出现中文乱码问题,这是因为java的zip方法不支持编码格式的更改,我们可以使用ant.java下的zip工具类来进行打包处理。所以需要将ant.jar导入项目的lib目录下。