这两天正好遇到解压问题,jdk里面正好有ZipEntity 可以使用,压缩包中有子目录的时候遇到了一些小的问题和坑记录一下。
必备知识:mkdir() 和mkdirs() 区别
文件夹的名字为newFolder, 假设 /home/admin/document/路径存在
mkdir() 该函数只能在已存在的文件夹下面建立新的文件夹,比如想创建 /home/admin/document/temp/newFolder 路径的文件夹, 如果/home/admin/document/temp/ 路径是存在的那么newFolder 就可以创建成功,如果/home/admin/document/temp/路径不存在则创建目录失败。
mkdirs() 该函数可以根据调用路径来建立路径 比如创建 /home/admin/document/temp/newFolder 程序编写者,不知道此文件路径/home/admin/document/temp 是否存在,就可以用此方法。该函数的原理是:如果父文件夹不存在并且最后一级子目录不存在,它就自动新建所有路径里写的文件夹;如果父文件夹存在,它就直接在已经存在的父文件夹下新建子文件夹。结果:/home/admin/document/temp/newFolder 先创建/home/admin/document/temp 文件夹 再创建 /home/admin/document/temp/newFolder/
完整的解压代码
package com.cn.passself.file;
import java.io.*;
import java.util.Enumeration;
import java.util.zip.ZipEntry;
import java.util.zip.ZipException;
import java.util.zip.ZipFile;
import java.util.zip.ZipInputStream;
/**
- Created by passself on 2018/7/11.
*/
public class ZipUtils {
public static void main(String[] args) {
String zipFilePath = "/Users/xxx/Documents/temp/v6.zip";
String outputFolder = "/Users/xxx/Documents/temp/res/";
System.out.println(new File(zipFilePath).exists());
unZipIt(zipFilePath,outputFolder);
//测试2
try {
upZipFile(new File(zipFilePath),outputFolder);
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 解压文件
* @param zipFilePath 解压文件路径
* @param outputFolder 输出解压文件路径
*/
public static void unZipIt(String zipFilePath,String outputFolder){
byte[] buffer = new byte[1024];
File folder = new File(outputFolder);
if (!folder.exists()){
folder.mkdir();
}
try {
//get the zip file content
ZipInputStream zis = new ZipInputStream(new FileInputStream(zipFilePath));
ZipEntry ze = zis.getNextEntry();
while (ze != null){
String fileName = ze.getName();
File newFile = new File(outputFolder+File.separator+fileName);
System.out.println("file unzip : "+ newFile.getAbsoluteFile());
//create all non exists folders
//else you will hit FileNotFoundException for compressed folder
//大部分网络上的源码,这里没有判断子目录
if (ze.isDirectory()){
newFile.mkdirs();
}else{
new File(newFile.getParent()).mkdirs();
FileOutputStream fos = new FileOutputStream(newFile);
int len;
while ((len = zis.read(buffer))!=-1){
fos.write(buffer,0,len);
}
fos.close();
}
ze = zis.getNextEntry();
}
zis.closeEntry();
zis.close();
System.out.println("Done");
}catch (IOException e){
e.printStackTrace();
}
}
public static void unzip(File source, String out) throws IOException {
try (ZipInputStream zis = new ZipInputStream(new FileInputStream(source))) {
ZipEntry entry = zis.getNextEntry();
while (entry != null) {
File file = new File(out, entry.getName());
if (entry.isDirectory()) {
file.mkdirs();
} else {
File parent = file.getParentFile();
if (!parent.exists()) {
parent.mkdirs();
}
try (BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(file))) {
byte[] buffer = new byte[Math.toIntExact(entry.getSize())];
int location;
while ((location = zis.read(buffer)) != -1) {
bos.write(buffer, 0, location);
}
}
}
entry = zis.getNextEntry();
}
}
}
/**
* 把所有文件都直接解压到指定目录(忽略子文件夹)
* @param zipFile
* @param folderPath
* @throws ZipException
* @throws IOException
*/
public static void upZipFile(File zipFile, String folderPath) throws ZipException, IOException {
File desDir = new File(folderPath);
if (!desDir.exists()) {
desDir.mkdirs();
}
ZipFile zf = new ZipFile(zipFile);
for (Enumeration<?> entries = zf.entries(); entries.hasMoreElements(); ) {
ZipEntry entry = ((ZipEntry) entries.nextElement());
InputStream in = zf.getInputStream(entry);
String str = folderPath;
// str = new String(str.getBytes("8859_1"), "GB2312");
File desFile = new File(str, java.net.URLEncoder.encode(entry.getName(), "UTF-8"));
if (!desFile.exists()) {
File fileParentDir = desFile.getParentFile();
if (!fileParentDir.exists()) {
fileParentDir.mkdirs();
}
}
OutputStream out = new FileOutputStream(desFile);
byte buffer[] = new byte[1024 * 1024];
int realLength = in.read(buffer);
while (realLength != -1) {
out.write(buffer, 0, realLength);
realLength = in.read(buffer);
}
out.close();
in.close();
}
}
}