android uzip 接口,Uzip folders recursively -android

在Android设备上尝试解压缩包含多个子文件夹和文件的zip文件时遇到问题。每次解压时都收到'No such file or directory'的错误。已经搜索并尝试了多种解决方案,包括使用ZipFile类进行解压,但仍然无法成功创建子文件夹。问题在于当遇到子文件夹时,系统找不到相应的路径。如何在解压缩zip文件时创建目标路径的子文件夹成为了一个挑战。
摘要由CSDN通过智能技术生成

I have tried to unzip a zipped file (This file contains many sub-folders and files).

I am not able to create sub-folders while unzipping the file.

Each time I am getting an error saying:

No such file or directory.

I have searched a lot about this like:

Android - Unzip a folder?

http://www.roseindia.net/tutorial/java/corejava/zip/ZipIsDirectory.html

Unzip a zipped file on sd card in Android application

How to unzip files recursively in Java?

But, nothing helped me.

Following is what I have tried:

public class UnZipper {

private static final String TAG = "UnZip";

private String mFileName, mDestinationPath;

public UnZipper(String fileName, String destinationPath) {

mFileName = fileName;

mDestinationPath = destinationPath;

}

public String getFileName() {

return mFileName;

}

public String getDestinationPath() {

return mDestinationPath;

}

// shrikant

public void unzip() {

String fullPath = mFileName;

Log.d(TAG, "unzipping " + mFileName + " to " + mDestinationPath);

doInBackground(fullPath, mDestinationPath);

}

// shrikant: I have changed return type from Boolean to boolean.

protected boolean doInBackground(String filePath, String destinationPath) {

File archive = new File(filePath);

boolean returnValue = false;

try {

ZipFile zipfile = new ZipFile(archive);

for (Enumeration e = zipfile.entries(); e.hasMoreElements();) {

ZipEntry entry = (ZipEntry) e.nextElement();

try {

unzipEntry(zipfile, entry, destinationPath);

Log.d("Unzipped", entry.getName());

returnValue = true;

} catch (Exception ex) {

Log.e(TAG,

"Error while extracting file: " + entry

+ ex.getMessage());

}

}

} catch (Exception e) {

Log.e(TAG, "Error while extracting file " + archive, e);

// return false;

}

return returnValue;

}

// shrikant: I have changed return type from void to boolean.

/**

* Unzips the zipped file into outputDir path.

*

* @param zipfile

* @param entry

* @param outputDir

* @throws IOException

*/

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

throws IOException {

Log.d("CURRENT ZIP", entry.getName());

String _dir = null, fileName = null;

if (entry.getName().contains("\\")) {

_dir = entry.getName().substring(0, entry.getName().indexOf('\\'));

createDir(new File(outputDir, _dir));

fileName = entry.getName().substring(entry.getName().indexOf('\\'));

}

// Change by Prashant : To Remove "/" from file Name Date : 5/01/2011

if (fileName.toString().startsWith("\\")) {

fileName = fileName.substring(1); // End

}

if (_dir != "")

outputDir = outputDir + "/" + _dir;

File outputFile = new File(outputDir, fileName);

if (!outputFile.getParentFile().exists()) {

createDir(outputFile.getParentFile());

}

Log.d("OUTPUT FILE", outputDir + fileName);

Log.v(TAG, "Extracting: " + entry);

Log.d("FOUND inside unzipEntry()", entry.getName());

BufferedInputStream inputStream = new BufferedInputStream(

zipfile.getInputStream(entry));

// **here I am getting error.**

BufferedOutputStream outputStream = new BufferedOutputStream(

new FileOutputStream(outputFile));

// **above line.**

try {

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("Cannot create dir " + dir);

}

}

private void copy(BufferedInputStream input, BufferedOutputStream output)

throws IOException {

byte[] buffer = new byte[4096];

int size;

while ((size = input.read(buffer)) != -1)

output.write(buffer, 0, size);

}

}

My question is: (Please look at the code)

When I am calling unzipEntry(), when it encounters a sub-folder, it passes something like "/data/abc.ext", but my file system does not contain any folder named as "data", I even tried to create it, but failed in doing so.

So how to create sub-folders from zipped file to destination path??

I have even tried method:

if(entry.isDirectory) {

// create directory

}

But this doesn't get called, since unzipEntry() (Please look in for() loop) directly passes the file under sub-folder.

Please help me on this.

Thank you.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值