gddhy

_(:з」∠)_ 加载中...
  • 主页
  • 归档
  • 工具
  • 关于
所有文章 友链

gddhy

_(:з」∠)_ 加载中...

  • 主页
  • 归档
  • 工具
  • 关于

unzip Javazip解压

2022-09-27
字数统计:473字 阅读时长≈2分

记录一个zip解压类,方便快速使用

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
import android.content.Context;
import android.net.Uri;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;

public class Unzip {
/**
* 解压assets的zip压缩文件到指定目录
* @param context 上下文对象
* @param assetName 压缩文件名
* @param outputDirectory 输出目录
* @param isReWrite 是否覆盖
* @throws IOException
*/
public static void unZip(Context context, String assetName, String outputDirectory, boolean isReWrite) throws IOException {
unZip(context.getAssets().open(assetName),outputDirectory,isReWrite);
}

/**
* 解压uri的zip压缩文件到指定目录
* @param context 上下文对象
* @param uri 压缩文件Uri
* @param outputDirectory 输出目录
* @param isReWrite 是否覆盖
* @throws IOException
*/
public static void unZip(Context context, Uri uri, String outputDirectory, boolean isReWrite) throws IOException {
unZip(context.getContentResolver().openInputStream(uri),outputDirectory,isReWrite);
}

/**
* 解压压缩文件到指定目录
* @param zipFile 压缩文件
* @param outputDirectory 输出目录
* @param isReWrite 是否覆盖
* @throws IOException
*/
public static void unZip(File zipFile, String outputDirectory, boolean isReWrite) throws IOException {
unZip(new FileInputStream(zipFile),outputDirectory,isReWrite);
}

private static void unZip(InputStream inputStream, String outputDirectory, boolean isReWrite) throws IOException {
// 创建解压目标目录
File file = new File(outputDirectory);
// 如果目标目录不存在,则创建
if (!file.exists()) {
file.mkdirs();
}
// 打开压缩文件
ZipInputStream zipInputStream = new ZipInputStream(inputStream);
// 读取一个进入点
ZipEntry zipEntry = zipInputStream.getNextEntry();
// 使用1Mbuffer
byte[] buffer = new byte[1024 * 1024];
// 解压时字节计数
int count = 0;
// 如果进入点为空说明已经遍历完所有压缩包中文件和目录
while (zipEntry != null) {
// 如果是一个目录
if (zipEntry.isDirectory()) {
file = new File(outputDirectory + File.separator + zipEntry.getName());
// 文件需要覆盖或者是文件不存在
if (isReWrite || !file.exists()) {
file.mkdir();
}
} else {
// 如果是文件
file = new File(outputDirectory + File.separator + zipEntry.getName());
// 文件需要覆盖或者文件不存在,则解压文件
if (isReWrite || !file.exists()) {
file.createNewFile();
FileOutputStream fileOutputStream = new FileOutputStream(file);
while ((count = zipInputStream.read(buffer)) > 0) {
fileOutputStream.write(buffer, 0, count);
}
fileOutputStream.close();
}
}
// 定位到下一个文件入口
zipEntry = zipInputStream.getNextEntry();
}
zipInputStream.close();
}
}
赏

谢谢你请我吃糖果

微信

扫一扫,分享到微信

微信分享二维码
代码中设置view大小
远程触发GithubAction
目录,不存在的…
留言已关闭
:gddhy
© gddhy
Hexo Theme Yilia by Litten
  • 所有文章
  • 友链

tag:

  • Android
  • 软件分享
  • game
  • Hexo
  • JavaScript
  • 旧机博物馆
  • MIUI
  • Java
  • git
  • Termux
  • mtk
  • 原神
  • Win
  • Html
  • 安卓学习笔记

    缺失模块

  • Luminous' Home
  • 影子博客
  • 四次元领域
  • 初之音
  • Mr.Pumpkin
  • ZhaoQuinn 's Blog