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 版本问题*/
打包压缩的如下: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()
下载的如下:
private int blockSize=65000
File file = new File(sourceFilePathName)
FileInputStream fileIn = new FileInputStream(file)
int readBytes = 0
readBytes = fileIn.read(b, 0, blockSize)
totalRead += readBytes
out.write(b, 0, readBytes)
代码大致如此,请参考。