// 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]
分享到:
相关推荐
在Java编程语言中,`ZipUtil`文件压缩工具类是一个非常实用的工具,它利用了Java内置的`java.util.zip`包中的`ZipEntry`类来实现文件或目录的压缩功能。`ZipUtil`通常被设计为一个静态类,提供一系列静态方法,使得...
`ZipUtil`是一个专门用于文件压缩的工具类,它解决了在处理包含中文字符的文件或目录时可能出现的乱码问题。`ZipEntry`是Java标准库`java.util.zip`包中的核心类,用于表示ZIP文件中的一个条目,它可以是文件或目录...
`ZIPUtil`可能是一个自定义的工具类,用于简化这个过程。以下是一些关于如何在Java中实现这个功能的关键知识点: 1. **Java档案API (Java Archive API, JAR)**: Java标准库提供了`java.util.zip`包,其中包含用于...
`ZipUtil.rar`这个文件很可能包含了一个Java库,用于处理ZIP压缩格式的文件操作,如遍历、解压和压缩。这里我们将深入探讨与Java、ZIP格式以及文件夹操作相关的知识点。 1. **Java I/O流**:Java提供了丰富的I/O流...
加密压缩 解密解压 java编写 不需jdk环境 exe双击即可运行
java的对于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 ...
Java压缩文件工具类ZipUtil使用方法代码示例 Java压缩文件工具类ZipUtil是一个功能强大且实用的压缩文件工具类,通过使用Java的Zip输入输出流实现压缩和解压文件。下面是对ZipUtil类的详细介绍和使用方法代码示例。...
java解压以及压缩zip,可运行程序!
利用java代码将多个图片一起打包下载工具类,实现语言为java,打包类型为zip格式;具体的实现方式可以参考文件中内容。
当需要向已存在的ZIP文件追加文件时,首先使用`ZipUtil`解压缩ZIP文件到一个临时目录,然后在该目录中添加新的文件,最后重新压缩整个目录并覆盖原有的ZIP文件。以下是大致的步骤: - 使用`ZipUtil`的`unzip`方法...
此工具类利用freemarker模板生成单个word文档到浏览器,同时支持多个word文档打包压缩后下载到浏览器,
ZipUtil.unzip("fileName", "fileDir"); fileName是文件的路径+文件名 fileDir是解压后的文件路径 2.生成zip文件 /** * 使用给定密码压缩指定文件或文件夹到指定位置. * <p> * dest可传最终压缩文件存放的...
ZipUtil.unpack(new File("/tmp/demo.zip"), new File("/tmp/demo"), new NameMapper() { public String map(String name) { return name.startsWith(prefix) ? name.substring(prefix.length()) ...
这里,我们重点讨论如何正确地处理中文字符以避免乱码,并介绍`ZipUtil`和`ZipEntry`这两个关键概念。 `ZipEntry`是Java `java.util.zip`包中的一个类,它代表ZIP档案中的一个条目。当我们创建一个新的`ZipEntry`...