我试图解压缩一个压缩文件(该文件包含许多子文件夹和文件)。
I am not able to create sub-folders while unzipping the file.
每次我收到一个错误说:
No such file or directory.
我对此进行了很多搜索,例如:
- Android - 解压缩文件夹?
- http://www.roseindia.net/tutorial/java/corejava/zip/ZipIsDirectory.html
- 在 Android 应用程序中解压缩 sd 卡上的压缩文件
- 如何在Java中递归解压缩文件?
但是,没有任何帮助。
以下是我尝试过的:
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);
}
}
我的问题是:(请看代码)
当我打电话unzipEntry()
时,当它遇到一个子文件夹时,它会传递类似“ /data/abc.ext
”的东西,但是我的文件系统不包含任何名为“ data ”的文件夹,我什至尝试创建它,但没有成功。
那么如何创建从压缩文件到目标路径的子文件夹?
我什至尝试过方法:
if(entry.isDirectory) {
// create directory
}
但这不会被调用,因为unzipEntry()
(请查看for() loop
)直接传递子文件夹下的文件。
请帮助我。
谢谢你。