1、ZipUtil

master
wangbing 6 years ago
parent 3dd95cd2c5
commit 855cf8a38b

@ -1,28 +1,24 @@
package xyz.wbsite.dbtool.javafx.tool;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.*;
import java.util.Enumeration;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import java.util.zip.ZipOutputStream;
public class ZipUtil {
/**
*
*
* @param zipFile
* @param zipFile Zip
* @param descDir
* @return
*/
public void unZip(File zipFile, File descDir) {
public static void unZip(File zipFile, File descDir) {
if (!descDir.exists()) {
descDir.mkdirs();
}
ZipFile zip = null;
System.out.println("解压文件" + zipFile.getAbsolutePath());
try {
zip = new ZipFile(zipFile);
Enumeration<?> entries = zip.entries();
@ -30,8 +26,7 @@ public class ZipUtil {
ZipEntry entry = (ZipEntry) entries.nextElement();
// 如果是文件夹,就创建个文件夹
if (entry.isDirectory()) {
String dirPath = descDir + "/" + entry.getName();
File dir = new File(dirPath);
File dir = new File(descDir, entry.getName());
dir.mkdirs();
} else {
// 如果是文件就先创建一个文件然后用io流把内容copy过去
@ -54,8 +49,9 @@ public class ZipUtil {
is.close();
}
}
System.out.println("解压文件" + zipFile.getAbsolutePath() + "成功");
} catch (Exception e) {
throw new RuntimeException("unzip error from ZipUtils", e);
throw new RuntimeException("unzip error from ZipUtil", e);
} finally {
if (zip != null) {
try {
@ -66,4 +62,77 @@ public class ZipUtil {
}
}
}
/**
*
*
* @param src
* @param zip Zip
*/
public static void toZip(File src, File zip) {
ZipOutputStream zipOutputStream = null;
try {
if (!src.exists()) {
System.err.println("压缩文件或目录不存在");
return;
}
if (!zip.exists()) {
zip.createNewFile();
}
zipOutputStream = new ZipOutputStream(new FileOutputStream(zip));
compress(src, zipOutputStream, null);
zipOutputStream.close();
System.out.println("压缩文件" + zip.getAbsolutePath() + "成功");
} catch (IOException e) {
System.err.println("压缩失败");
e.printStackTrace();
} finally {
if (zipOutputStream != null) {
try {
zipOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
/**
* @param sourceFile
* @param zos Zip
* @param parentPath
* @throws IOException
*/
private static void compress(File sourceFile, ZipOutputStream zos, String parentPath) throws IOException {
byte[] buff = new byte[1024];
if (sourceFile.isDirectory()) {
//确保空文件夹也可以压缩进去
zos.putNextEntry(new ZipEntry((parentPath != null ? parentPath : "") + sourceFile.getName() + "/"));
zos.closeEntry();
//循环压缩子文件或文件夹
for (File file : sourceFile.listFiles()) {
compress(file, zos, (parentPath != null ? parentPath : "") + sourceFile.getName() + "/");
}
} else {
//压缩文件
zos.putNextEntry(new ZipEntry((parentPath != null ? parentPath : "") + sourceFile.getName()));
int len;
FileInputStream in = new FileInputStream(sourceFile);
while ((len = in.read(buff)) != -1) {
zos.write(buff, 0, len);
}
zos.closeEntry();
in.close();
}
}
public static void main(String[] args) {
//解压
unZip(new File("E:\\AAA.zip"), new File("E:\\AAA"));
//压缩
toZip(new File("E:\\AAA"), new File("E:\\AAA.zip"));
}
}

Loading…
Cancel
Save

Powered by TurnKey Linux.