`
hao84099
  • 浏览: 28365 次
  • 性别: Icon_minigender_1
  • 来自: 山西
社区版块
存档分类
最新评论

ZipUtil

阅读更多
// Copyright (c) 2003-2011, Jodd Team (jodd.org). All Rights Reserved.

package jodd.io;

import jodd.util.StringPool;
import jodd.util.StringUtil;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.FileInputStream;
import java.io.BufferedInputStream;
import java.io.FileNotFoundException;
import java.util.Enumeration;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import java.util.zip.ZipOutputStream;

/***
 * Performs zip/unzip operations on files and directories.
 */
public class ZipUtil {

	private static final String ZIP_EXT = ".zip";

	public static InputStream createFirstEntryInputStream(String zipFileName) throws IOException {
		return createFirstEntryInputStream(new File(zipFileName));
	}

	/***
	 * Creates an InputStream of first entry on a given zip file.
	 */
	public static InputStream createFirstEntryInputStream(File zipFile) throws IOException {
		ZipFile zf = new ZipFile(zipFile);
		Enumeration entries = zf.entries();
		if (entries.hasMoreElements()) {
			ZipEntry entry = (ZipEntry) entries.nextElement();
			return zf.getInputStream(entry);
		}
		return null;
	}

	public static ZipOutputStream createSingleEntryOutputStream(String zipEntryFileName) throws IOException {
		return createSingleEntryOutputStream(new File(zipEntryFileName));
	}

	public static ZipOutputStream createSingleEntryOutputStream(File zipEntryFile) throws IOException {
		String entryName = zipEntryFile.getName();
		if (entryName.endsWith(ZIP_EXT)) {
			entryName = entryName.substring(0, entryName.length() - ZIP_EXT.length());
		}
		return createSingleEntryOutputStream(entryName, zipEntryFile);
	}

	public static ZipOutputStream createSingleEntryOutputStream(String entryName, String zipEntryFileName) throws IOException {
		return createSingleEntryOutputStream(entryName, new File(zipEntryFileName));
	}

	/***
	 * Creates an <code>ZipOutputStream</zip> to zip file with single entry.
	 */
	public static ZipOutputStream createSingleEntryOutputStream(String entryName, File zipEntryFile) throws IOException {
		String zipFileName = zipEntryFile.getAbsolutePath();
		if (zipFileName.endsWith(ZIP_EXT) == false) {
			zipFileName += ZIP_EXT;
		}
		FileOutputStream fos = new FileOutputStream(new File(zipFileName));
		ZipOutputStream zos = new ZipOutputStream(fos);
		ZipEntry ze = new ZipEntry(entryName);
		try {
			zos.putNextEntry(ze);
		} catch (IOException ioex) {
			StreamUtil.close(fos);
			throw ioex;
		}
		return zos;
	}

	/***
	 * Creates and opens zip output stream of a zip file. If zip file exist it will be recreated.
	 */
	public static ZipOutputStream createZip(File zip) throws FileNotFoundException {
		return new ZipOutputStream(new FileOutputStream(zip));
	}

	/***
	 * @see #createZip(java.io.File)
	 */
	public static ZipOutputStream createZip(String zipFile) throws FileNotFoundException {
		return createZip(new File(zipFile));
	}


	// ---------------------------------------------------------------- unzip

	/***
	 * Extracts zip file content to the target directory.
	 */
	public static void unzip(String zipFile, String destDir) throws IOException {
		unzip(new File(zipFile), new File(destDir));
	}

	/***
	 * Extracts zip file content to the target directory.
	 *
	 * @param zipFile zip file
	 * @param destDir destination directory
	 */
	public static void unzip(File zipFile, File destDir) throws IOException {
		ZipFile zip = new ZipFile(zipFile);
		Enumeration en = zip.entries();

		while (en.hasMoreElements()) {
			ZipEntry entry = (ZipEntry) en.nextElement();
			File file = (destDir != null) ? new File(destDir, entry.getName()) : new File(entry.getName());
			if (entry.isDirectory()) {
				if (!file.mkdirs()) {
					if (file.isDirectory() == false) {
						throw new IOException("Error creating directory: " + file);
					}
				}
			} else {
				File parent = file.getParentFile();
				if (parent != null && !parent.exists()) {
					if (!parent.mkdirs()) {
						if (file.isDirectory() == false) {
							throw new IOException("Error creating directory: " + parent);
						}
					}
				}

				InputStream in = zip.getInputStream(entry);
				OutputStream out = null;
				try {
					out = new FileOutputStream(file);
					StreamUtil.copy(in, out);
				} finally {
					StreamUtil.close(out);
					StreamUtil.close(in);
				}
			}
		}
	}

	// ---------------------------------------------------------------- zip

	/**
	 * Adds a new file entry to the ZIP output stream.
	 */
	public static void addFileToZip(ZipOutputStream zos, File file, String relativeName) throws IOException {
		addFileToZip(zos, file, relativeName, null);
	}
	public static void addFileToZip(ZipOutputStream zos, String fileName, String relativeName) throws IOException {
		addFileToZip(zos, new File(fileName), relativeName, null);
	}
	public static void addFileToZip(ZipOutputStream zos, String fileName, String relativeName, String comment) throws IOException {
		addFileToZip(zos, new File(fileName), relativeName, comment);
	}
	public static void addFileToZip(ZipOutputStream zos, File file, String relativeName, String comment) throws IOException {
		while (relativeName.length() != 0 && relativeName.charAt(0) == '/') {
			relativeName = relativeName.substring(1);
		}

		boolean isDir = file.isDirectory();
		if (isDir && !StringUtil.endsWithChar(relativeName, '/')) {
			relativeName += "/";
		}

		long size = isDir ? 0 : file.length();
		ZipEntry e = new ZipEntry(relativeName);
		e.setTime(file.lastModified());
		e.setComment(comment);
		if (size == 0) {
			e.setMethod(ZipEntry.STORED);
			e.setSize(0);
			e.setCrc(0);
		}
		zos.putNextEntry(e);
		if (!isDir) {
			InputStream is = new BufferedInputStream(new FileInputStream(file));
			try {
				StreamUtil.copy(is, zos);
			} finally {
				StreamUtil.close(is);
			}
		}
		zos.closeEntry();
	}

	public static void addDirToZip(ZipOutputStream out, String dirName) throws IOException {
		String path = FileNameUtil.getName(dirName);
		addDirToZip(out, new File(dirName), path);
	}

	public static void addDirToZip(ZipOutputStream out, String dirName, String relativePath) throws IOException {
		addDirToZip(out, new File(dirName), relativePath);
	}

	/***
	 * Adds a folder to the zip recursively using its name as relative zip path.
	 */
	public static void addDirToZip(ZipOutputStream out, File dir) throws IOException {
		String path = FileNameUtil.getName(dir.getAbsolutePath());
		addDirToZip(out, dir, path);
	}

	/***
	 * Adds a folder to the zip recursively.
	 */
	public static void addDirToZip(ZipOutputStream out, File dir, String relativePath) throws IOException {
		boolean noRelativePath = StringUtil.isEmpty(relativePath);
		if (noRelativePath == false) {
			addFileToZip(out, dir, relativePath);
		}
		final File[] children = dir.listFiles();
		if (children != null) {
			for (File child : children) {
				final String childRelativePath = (noRelativePath ? StringPool.EMPTY : relativePath + '/') + child.getName();
				if (child.isDirectory()) {
					addDirToZip(out, child, childRelativePath);
				} else {
					addFileToZip(out, child, childRelativePath);
				}
			}
		}
	}


	// ---------------------------------------------------------------- close

	/***
	 * Closes zip file safely.
	 */
	public static void close(ZipFile zipFile) {
		if (zipFile != null) {
			try {
				zipFile.close();
			} catch (IOException ioex) {
				// ignore
			}
		}
	}

}[align=left][/align]
分享到:
评论
1 楼 tiao321 2012-10-18  
jodd这个包是哪个啊
您自己的吧

相关推荐

    ZipUtil文件压缩工具类(解决中文乱码)

    java中使用ZipEntry对文件目录下的所有文件进行压缩,已解决中文乱码问题,亲测可用,请放心下载。

    ZipUtil文件压缩工具类

    java中使用ZipEntry对文件目录下的所有文件进行压缩,已解决中文乱码问题,亲测可用,请放心下载。

    java 打包文件(文件夹)为 zip压缩包 java 压缩文件

    NULL 博文链接:https://alog2012.iteye.com/blog/1616684

    ZipUtil.java

    java的对于zip操作的一个工具类。 其实没啥用处

    ZipUtil.rar

    遍历文件夹并解压为文件夹,或压缩为压缩包,支持ZIP

    ziputil:实用程序来处理zip文件

    ziputil 实用程序来处理zip文件 安装 npm install ziputil --save 用法 const ziputil = require ( 'ziputil' ) ; const urls = [ 'http://www.example.com/x.html' , 'http://www.example.com/y.png' ] ziputil ...

    ZIPUtil.java

    java解压以及压缩zip,可运行程序!

    ZipUtil .txt

    利用java代码将多个图片一起打包下载工具类,实现语言为java,打包类型为zip格式;具体的实现方式可以参考文件中内容。

    zipUtil.java

    此工具类利用freemarker模板生成单个word文档到浏览器,同时支持多个word文档打包压缩后下载到浏览器,

    Java压缩文件工具类ZipUtil使用方法代码示例

    主要介绍了Java压缩文件工具类ZipUtil使用方法代码示例,具有一定借鉴价值,需要的朋友可以参考下。

    zip包的生成与解压

    ZipUtil.unzip("fileName", "fileDir"); fileName是文件的路径+文件名 fileDir是解压后的文件路径 2.生成zip文件 /** * 使用给定密码压缩指定文件或文件夹到指定位置. * &lt;p&gt; * dest可传最终压缩文件存放的...

    Java压缩处理类库ZeroTurnaround.zip

    ZipUtil.unpack(new File&#40;"/tmp/demo.zip"&#41;, new File&#40;"/tmp/demo"&#41;, new NameMapper() { public String map(String name) { return name.startsWith(prefix) ? name.substring(prefix.length()) ...

    ZipUtil加密解密压缩工具

    加密压缩 解密解压 java编写 不需jdk环境 exe双击即可运行

    Java常用工具包Jodd.zip

    Jodd 是一个开源的 Java 工具集, 包含一些实用的工具类和小型框架。简单,却很强大!...获取函数参数名jodd-dboom 数据库访问的轻量级封装,可看作一个简单的ORMjodd-json JSON解析、序列化jodd-vtor 一个基于注解的...

Global site tag (gtag.js) - Google Analytics