阅读:4409回复:0
以JDK自带的方式打包文件夹为ZIP实例
在开发过程中,会遇到需要将整个文件夹(包括嵌套文件夹及其文件)打包的需求,这里有两种方式可以参考,第一种是以JDK自带的方式打包,第二种是ant.jar的技术打包。第二种技术将在下一个帖子发表。
import java.io.BufferedInputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.util.zip.CRC32; import java.util.zip.CheckedOutputStream; import java.util.zip.ZipEntry; import java.util.zip.ZipOutputStream; /** * ZIP压缩工具 * * @author <a href="mailto:zlex.dongliang@gmail.com">梁栋</a> * @since 1.0 */ publicclass ZipUtils { publicstaticfinal String EXT = ".zip"; privatestaticfinal String BASE_DIR = ""; // 符号"/"用来作为目录标识判断符 privatestaticfinal String PATH = "/"; privatestaticfinalintBUFFER = 1024; /** * 压缩 * * @param srcFile * @throws Exception */ publicstaticvoid compress(File srcFile) throws Exception { String name = srcFile.getName(); System.err.println(name); String basePath = srcFile.getParent(); System.err.println(basePath); String destPath = basePath +"/"+ name + EXT; System.err.println(destPath); compress(srcFile, destPath); } /** * 压缩 * * @param srcFile * 源路径 * @param destPath * 目标路径 * @throws Exception */ publicstaticvoid compress(File srcFile, File destFile) throws Exception { // 对输出文件做CRC32校验 CheckedOutputStream cos = new CheckedOutputStream(new FileOutputStream( destFile), new CRC32()); ZipOutputStream zos = new ZipOutputStream(cos); compress(srcFile, zos, BASE_DIR); zos.flush(); zos.close(); } /** * 压缩文件 * * @param srcFile * @param destPath * @throws Exception */ publicstaticvoid compress(File srcFile, String destPath) throws Exception { compress(srcFile, new File(destPath)); } /** * 压缩 * * @param srcFile * 源路径 * @param zos * ZipOutputStream * @param basePath * 压缩包内相对路径 * @throws Exception */ privatestaticvoid compress(File srcFile, ZipOutputStream zos, String basePath) throws Exception { if (srcFile.isDirectory()) { compressDir(srcFile, zos, basePath); } else { compressFile(srcFile, zos, basePath); } } /** * 压缩 * * @param srcPath * @throws Exception */ publicstaticvoid compress(String srcPath) throws Exception { File srcFile = new File(srcPath); compress(srcFile); } /** * 文件压缩 * * @param srcPath * 源文件路径 * @param destPath * 目标文件路径 * */ publicstaticvoid compress(String srcPath, String destPath) throws Exception { File srcFile = new File(srcPath); compress(srcFile, destPath); } /** * 压缩目录 * * @param dir * @param zos * @param basePath * @throws Exception */ privatestaticvoid compressDir(File dir, ZipOutputStream zos, String basePath) throws Exception { File[] files = dir.listFiles(); // 构建空目录 if (files.length < 1) { ZipEntry entry = new ZipEntry(basePath + dir.getName() + PATH); zos.putNextEntry(entry); zos.closeEntry(); } for (File file : files) { // 递归压缩 compress(file, zos, basePath + dir.getName() + PATH); } } /** * 文件压缩 * * @param file * 待压缩文件 * @param zos * ZipOutputStream * @param dir * 压缩文件中的当前路径 * @throws Exception */ privatestaticvoid compressFile(File file, ZipOutputStream zos, String dir) throws Exception { /** * 压缩包内文件名定义 * * <pre> * 如果有多级目录,那么这里就需要给出包含目录的文件名 * 如果用WinRAR打开压缩包,中文名将显示为乱码 * </pre> */ ZipEntry entry = new ZipEntry(dir + file.getName()); zos.putNextEntry(entry); BufferedInputStream bis = new BufferedInputStream(new FileInputStream( file)); intcount; bytedata[] = newbyte[BUFFER]; while ((count = bis.read(data, 0, BUFFER)) != -1) { zos.write(data, 0, count); } bis.close(); zos.closeEntry(); } publicstaticvoid main(String[] args) throws Exception { // 压缩文件 //ZipUtils.compress("d:\\f.txt"); // 压缩目录 ZipUtils.compress("/Users/zhangshuai/Desktop/WebRoot/"); } } |
|