📄 ziputils.java
字号:
package com.easyjf.web.tools;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Enumeration;
import org.apache.tools.zip.ZipEntry;
import org.apache.tools.zip.ZipOutputStream;
/**
* 实现对ZIP文件进行解压缩
*
* 使用时需要下载ant.jar包导入lib下
*
* @author NetGod
*
*/
public class ZipUtils {
public ZipUtils() {
}
/**
* zip方法对文件或者文件夹进行压缩
*
* @param zipstringPath
* 为要压缩的文件或者文件夹路径
* @param zipFileName
* 为压缩之后生成的文件名
*/
public void zip(String zipstringPath, String zipFileName) throws Exception {
try {
byte[] buf = new byte[1024];
File zipPath = new File(zipstringPath);
int filelen = zipPath.listFiles().length;
String[] filenames = new String[filelen];
try {
File[] files = zipPath.listFiles();
for (int i = 0; i < filelen; i++) {
filenames[i] = zipPath.getPath() + File.separator
+ files[i].getName();
}
ZipOutputStream out = new ZipOutputStream(new FileOutputStream(
zipFileName));
for (int i = 0; i < filenames.length; i++) {
FileInputStream in = new FileInputStream(filenames[i]);
out.putNextEntry(new ZipEntry(files[i].getName()));
int len;
while ((len = in.read(buf)) > 0) {
out.write(buf, 0, len);
}
out.closeEntry();
in.close();
}
out.close();
} catch (IOException e) {
System.out.println(e);
}
} catch (Exception ex) {
ex.printStackTrace(System.out);
}
}
/**
* unzip方法对文件进行解压缩
*
* @param zipFileName
* 需要解压缩的文件名
* @param outputDirectory
* 解压之后的目标路径
*/
public void unzip(String zipFileName, String outputDirectory)
throws Exception {
try {
org.apache.tools.zip.ZipFile zipFile = new org.apache.tools.zip.ZipFile(
zipFileName);
Enumeration e = zipFile.getEntries();
ZipEntry zipEntry = null;
while (e.hasMoreElements()) {
zipEntry = (ZipEntry) e.nextElement();
System.out.println("unziping:" + zipEntry.getName());
if (zipEntry.isDirectory()) {
String name = zipEntry.getName();
name = name.substring(0, name.length() - 1);
System.out.println("OutputPath:" + outputDirectory
+ File.separator + name);
File f = new File(outputDirectory + File.separator + name);
f.mkdir();
System.out.println("CreateDirectory:" + outputDirectory
+ File.separator + name);
} else {
File f = new File(outputDirectory + File.separator
+ zipEntry.getName());
f.createNewFile();
InputStream in = zipFile.getInputStream(zipEntry);
FileOutputStream out = new FileOutputStream(f);
int c;
byte[] by = new byte[1024];
while ((c = in.read(by)) != -1) {
out.write(by, 0, c);
}
out.close();
in.close();
}
}
} catch (Exception ex) {
ex.printStackTrace(System.out);
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -