BeanSoft's Java Blog
免费电子书/视频《MyEclipse 6 Java 开发中文教程》作者刘长炯官方博客

来自: http://beansoft.java-cn.org/Wiki.jsp?page=Beansoft.jar.ZipUtil

用 Java 打包目录为 ZIP 文件

其实还有其它的功能, 例如创建新 ZIP, 添加文件, 添加目录等, 下一步再加上. 这个类主要目的是为了便于备份 beansoft.java-cn.org 上的 jspwiki 页面目录.

例如本站的 jspwiki 页面的所有内容可以通过下载内容页面压缩包 http://beansoft.java-cn.org/download/jspwiki_pages.zip 4MB 解压缩到本机wiki页面目录即可浏览, 便于所有成员共享自己编写的资料.

				
/* * @(#)ZipUtil.java 1.00 2007-2-15 * * Copyright 2007 BeanSoft Studio. All rights reserved. * PROPRIETARY/CONFIDENTIAL. Use is subject to license terms. */ package beansoft.jar; import java.io.*; import java.util.jar.*; /** * ZipUtil, a Zip util class created by BeanSoft(beansoft@126.com). * * Chinese documents:<br/> * 一个 ZIP 工具类, BeanSoft(beansoft@126.com) 创建. * * @author BeanSoft * @version 1.00 2007-2-15 */ public class ZipUtil { /** * 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(JarOutputStream jos, File file, String pathName) throws IOException, FileNotFoundException { if (file.isDirectory()) { pathName = pathName + file.getName() + "/"; jos.putNextEntry(new JarEntry(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 { JarEntry jarEntry = new JarEntry(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(); } } /** * 创建 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 = ""; } JarOutputStream jos = new JarOutputStream(new FileOutputStream(zipFile), new Manifest()); 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; } }

用法示例(JSP页面, 将站点的 /jspwiki 目录下的所有文件打包为 download/jspwiki_pages.zip):

				
<% String homeDir = application.getRealPath("."); java.io.File zipFile = new java.io.File(homeDir, "download" + java.io.File.separatorChar + "jspwiki_pages.zip"); java.io.File pagesDirectory = new java.io.File(homeDir, "jspwiki"); out.println("Making zip file from folder /jspwiki to " + zipFile); out.println("<br/>"); beansoft.jar.ZipUtil.makeDirectoryToZip(pagesDirectory, zipFile, null, 9); out.println("Zip file " + zipFile + " has been made."); %> <br/> <a href="./download/jspwiki_pages.zip">Download ZIP File</a>
posted on 2007-02-16 11:11 BeanSoft 阅读(1211) 评论(0)  编辑  收藏 所属分类: JSPWiki

标题  
姓名  
主页
验证码 *  
内容(请不要发表任何与政治相关的内容)  
  登录  使用高级评论  新用户注册  返回页首  恢复上次提交      
该文被作者在 2007-02-16 13:07 编辑过
 
 

推荐图书:
走出软件作坊》、《悟透JavaScript》、《Head First 设计模式
相关链接:
网站导航: