java解压zip
目前有个需求,需要解压zip包,怎么做呢,我也不知道,先百度。
然后我发现了java.util.zip这个包,java自带这个包有没有!!百度上面虽然都是片段代码但是我顺藤摸瓜找到了java.util.zip。那么我们来看看这个目录的结构
思路:
获取zip文件里面的文件目录和文件信息,获取文件的流,然后通过File类创建并写入流就能实现解压了。
下面demo的使用
在D盘创建一个正常的full.zip文件就可以直接解压到d:/code这个目录下面。
然后运行下面的main方法可以解压zip文件了。
public class HeHe {
public static void main(String[] args) {
File parent = new File("d:/code");
try {
//也就是说这个文件后缀是不是zip
ZipFile zipFile = new ZipFile("d:/full.zip");
System.out.println(zipFile.stream());
Enumeration<? extends ZipEntry> entries = zipFile.entries();
while (entries.hasMoreElements()) {
ZipEntry zipEntry = entries.nextElement();
File file = new File(parent, zipEntry.getName());
if (zipEntry.isDirectory()) {
mkdirs(file);
} else {
createFile(file);
InputStream is = zipFile.getInputStream(zipEntry);
FileOutputStream fos = new FileOutputStream(file);
byte[] b = new byte[1024];
int len = 0;
while ((len = is.read(b)) != -1) {
fos.write(b, 0, len);
}
}
System.out.println(zipEntry.getName());
}
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 创建文件
*
* @param path 文件字符串路径例如d:/fulld/why/ass/a/asd
*/
public static void createFile(String path) {
createFile(new File(path));
}
/**
* 创建文件
*
* @param file file类
*/
private static void createFile(File file) {
if (file.exists() && file.isFile()) {
file.delete();
try {
file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
return;
}
File parentFile = file.getParentFile();
if (parentFile.exists()) {
if (parentFile.isFile()) {
parentFile.delete();
parentFile.mkdirs();
}
} else {
parentFile.mkdirs();
}
try {
file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 创建文件夹
*
* @param path 文件夹的路径字符串例如d:/fulld/why/ass/a/asd/
* @return 如果本来就存在或者创建成功,那么就返回true
*/
public static void mkdirs(String path) {
mkdirs(new File(path));
}
/**
* 创建文件夹
*
* @param file file类
*/
public static void mkdirs(File file) {
if (file.exists() && file.isDirectory()) {
return;
}
if (file.exists()) {
file.delete();
file.mkdirs();
} else {
file.mkdirs();
}
}
}
效果图:
然后我给大家整理了下上面的代码作为工具,使用方法很简单,decompressed是解压,三个方法根据自己的需求使用,已经在安卓系统上试过了,没有任何问题可以使用,java跨平台。
public class ZipUtils {
/**
* 解压方法
*
* @param parent 解压到目录下
* @param zip zip文件
*/
public static void decompressed(String parent, String zip) {
try {
decompressed(new File(parent), new ZipFile(zip));
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 解压方法
*
* @param parent 解压到目录下
* @param zip zip文件
*/
public static void decompressed(File parent, File zip) {
try {
decompressed(parent, new ZipFile(zip));
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 解压方法
*
* @param parent 解压到目录下
* @param zipFile zip文件
*/
public static void decompressed(File parent, ZipFile zipFile) {
mkdirs(parent);
try {
Enumeration<? extends ZipEntry> entries = zipFile.entries();
while (entries.hasMoreElements()) {
ZipEntry zipEntry = entries.nextElement();
File file = new File(parent, zipEntry.getName());
if (zipEntry.isDirectory()) {
mkdirs(file);
} else {
createFile(file);
InputStream is = zipFile.getInputStream(zipEntry);
FileOutputStream fos = new FileOutputStream(file);
byte[] b = new byte[1024];
int len;
while ((len = is.read(b)) != -1) {
fos.write(b, 0, len);
}
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 创建文件顺便创建父目录
*
* @param path 文件字符串路径例如d:/fulld/why/ass/a/asd
*/
public static void createFile(String path) {
createFile(new File(path));
}
/**
* 创建文件顺便创建父目录
*
* @param file file类
*/
public static void createFile(File file) {
if (file.exists() && file.isFile()) {
file.delete();
try {
file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
return;
}
File parentFile = file.getParentFile();
if (parentFile.exists()) {
if (parentFile.isFile()) {
parentFile.delete();
parentFile.mkdirs();
}
} else {
parentFile.mkdirs();
}
try {
file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 创建文件夹顺便创建父目录
*
* @param path 文件夹的路径字符串例如d:/fulld/why/ass/a/asd/
* @return 如果本来就存在或者创建成功,那么就返回true
*/
public static void mkdirs(String path) {
mkdirs(new File(path));
}
/**
* 创建文件夹顺便创建父目录
*
* @param file file类
*/
public static void mkdirs(File file) {
if (file.exists() && file.isDirectory()) {
return;
}
if (file.exists()) {//肯定是文件了,文件删掉
file.delete();
file.mkdirs();
} else {
file.mkdirs();
}
}
}