gpt4 book ai didi

Android - 解压缩文件夹?

转载 作者:塔克拉玛干 更新时间:2023-11-01 21:49:09 26 4
gpt4 key购买 nike

我的 SD 卡上有一个 zip 文件夹,如何解压缩该文件夹(在我的应用程序代码中)?

最佳答案

我正在使用初学者方法的修改版本,它扩展了 AsyncTask 并且可以在主线程上更新观察者。逐字节压缩非常慢,应该避免。相反,更有效的方法是将大块数据复制到输出流。

package com.blarg.webviewscroller;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Enumeration;
import java.util.Observable;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;

import org.apache.commons.io.IOUtils;

import android.os.AsyncTask;
import android.util.Log;

public class UnZipper extends Observable {

private static final String TAG = "UnZip";
private String mFileName, mFilePath, mDestinationPath;

public UnZipper (String fileName, String filePath, String destinationPath) {
mFileName = fileName;
mFilePath = filePath;
mDestinationPath = destinationPath;
}

public String getFileName () {
return mFileName;
}

public String getFilePath() {
return mFilePath;
}

public String getDestinationPath () {
return mDestinationPath;
}

public void unzip () {
String fullPath = mFilePath + "/" + mFileName + ".zip";
Log.d(TAG, "unzipping " + mFileName + " to " + mDestinationPath);
new UnZipTask().execute(fullPath, mDestinationPath);
}

private class UnZipTask extends AsyncTask<String, Void, Boolean> {

@SuppressWarnings("rawtypes")
@Override
protected Boolean doInBackground(String... params) {
String filePath = params[0];
String destinationPath = params[1];

File archive = new File(filePath);
try {
ZipFile zipfile = new ZipFile(archive);
for (Enumeration e = zipfile.entries(); e.hasMoreElements();) {
ZipEntry entry = (ZipEntry) e.nextElement();
unzipEntry(zipfile, entry, destinationPath);
}
} catch (Exception e) {
Log.e(TAG, "Error while extracting file " + archive, e);
return false;
}

return true;
}

@Override
protected void onPostExecute(Boolean result) {
setChanged();
notifyObservers();
}

private void unzipEntry(ZipFile zipfile, ZipEntry entry,
String outputDir) throws IOException {

if (entry.isDirectory()) {
createDir(new File(outputDir, entry.getName()));
return;
}

File outputFile = new File(outputDir, entry.getName());
if (!outputFile.getParentFile().exists()) {
createDir(outputFile.getParentFile());
}

Log.v(TAG, "Extracting: " + entry);
BufferedInputStream inputStream = new BufferedInputStream(zipfile.getInputStream(entry));
BufferedOutputStream outputStream = new BufferedOutputStream(new FileOutputStream(outputFile));

try {
IOUtils.copy(inputStream, outputStream);
} finally {
outputStream.close();
inputStream.close();
}
}

private void createDir(File dir) {
if (dir.exists()) {
return;
}
Log.v(TAG, "Creating dir " + dir.getName());
if (!dir.mkdirs()) {
throw new RuntimeException("Can not create dir " + dir);
}
}
}
}

它被一个实现Observer的类使用,例如:

private void unzipWebFile(String filename) {
String unzipLocation = getExternalFilesDir(null) + "/unzipped";
String filePath = Environment.getExternalStorageDirectory().toString();

UnZipper unzipper = new UnZipper(filename, filePath, unzipLocation);
unzipper.addObserver(this);
unzipper.unzip();
}

当解压缩完成时,您的观察者将获得一个 update(Observable observable, Object data) 回调。

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

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