1、Zip解压打包

Former-commit-id: d11c0274d05b3fba3448b7f9262180a04fc72a90
master
wangbing 5 years ago
parent 87b46f0de1
commit 7e25c513e4

@ -0,0 +1,145 @@
package ${basePackage}.frame.utils;
import java.io.*;
import java.util.Enumeration;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import java.util.zip.ZipOutputStream;
/**
* ZipUtil - Zip
*
* @author wangbing
* @version 0.0.1
* @since 2017-01-01
*/
public class ZipUtil {
/**
*
*
* @param zipFile Zip
* @param descDir
*/
public static void unZip(File zipFile, File descDir) {
if (!descDir.exists()) {
descDir.mkdirs();
}
ZipFile zip = null;
try {
zip = new ZipFile(zipFile);
Enumeration<?> entries = zip.entries();
while (entries.hasMoreElements()) {
ZipEntry entry = (ZipEntry) entries.nextElement();
// 如果是文件夹,就创建个文件夹
if (entry.isDirectory()) {
File dir = new File(descDir, entry.getName());
dir.mkdirs();
} else {
// 如果是文件就先创建一个文件然后用io流把内容copy过去
File targetFile = new File(descDir + "/" + entry.getName());
// 保证这个文件的父文件夹必须要存在
if (!targetFile.getParentFile().exists()) {
targetFile.getParentFile().mkdirs();
}
targetFile.createNewFile();
// 将压缩文件内容写入到这个文件中
InputStream is = zip.getInputStream(entry);
FileOutputStream fos = new FileOutputStream(targetFile);
int len;
byte[] buf = new byte[1024];
while ((len = is.read(buf)) != -1) {
fos.write(buf, 0, len);
}
// 关流顺序,先打开的后关闭
fos.close();
is.close();
}
}
System.out.println("解压文件" + zipFile.getAbsolutePath() + "成功");
} catch (Exception e) {
throw new RuntimeException("unzip error from ZipUtil", e);
} finally {
if (zip != null) {
try {
zip.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
/**
*
*
* @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.