gpt4 book ai didi

android - 如何压缩和解压缩文件?

转载 作者:IT老高 更新时间:2023-10-28 21:41:55 26 4
gpt4 key购买 nike

如何压缩和解压缩 DDMS 中已经存在的文件:data/data/mypackage/files/ 我需要一个简单的例子。我已经搜索了与 zip 和 unzip 相关的内容。但是,没有一个适合我的例子。谁能举个例子。提前谢谢。

最佳答案

查看 java.util.zip.* 类的 zip 功能。我已经完成了一些基本的 zip/unzip 代码,我将其粘贴在下面。希望对您有所帮助。

public static void zip(String[] files, String zipFile) throws IOException {
BufferedInputStream origin = null;
ZipOutputStream out = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(zipFile)));
try {
byte data[] = new byte[BUFFER_SIZE];

for (int i = 0; i < files.length; i++) {
FileInputStream fi = new FileInputStream(files[i]);
origin = new BufferedInputStream(fi, BUFFER_SIZE);
try {
ZipEntry entry = new ZipEntry(files[i].substring(files[i].lastIndexOf("/") + 1));
out.putNextEntry(entry);
int count;
while ((count = origin.read(data, 0, BUFFER_SIZE)) != -1) {
out.write(data, 0, count);
}
}
finally {
origin.close();
}
}
}
finally {
out.close();
}
}

public static void unzip(String zipFile, String location) throws IOException {
try {
File f = new File(location);
if(!f.isDirectory()) {
f.mkdirs();
}
ZipInputStream zin = new ZipInputStream(new FileInputStream(zipFile));
try {
ZipEntry ze = null;
while ((ze = zin.getNextEntry()) != null) {
String path = location + ze.getName();

if (ze.isDirectory()) {
File unzipFile = new File(path);
if(!unzipFile.isDirectory()) {
unzipFile.mkdirs();
}
}
else {
FileOutputStream fout = new FileOutputStream(path, false);
try {
for (int c = zin.read(); c != -1; c = zin.read()) {
fout.write(c);
}
zin.closeEntry();
}
finally {
fout.close();
}
}
}
}
finally {
zin.close();
}
}
catch (Exception e) {
Log.e(TAG, "Unzip exception", e);
}
}

关于android - 如何压缩和解压缩文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7485114/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com