我有一個不錯的java代碼,解壓縮.zip文件。但這個代碼的問題是Java:解壓縮文件,不創建目標文件夾
- 我需要在運行此代碼之前創建目標文件夾(注意:只有文件夾不是文件)。
- 否則我會得到路徑未找到異常。
因此,如果以前不知道壓縮文件內容,此代碼將無法正常工作。所以我認爲這是無用的代碼。任何人有更好的邏輯?或需要編輯Bellow代碼?
package com.mireader;
import android.os.Environment;
import android.util.Log;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
/**
*
* @author jon
*/
public class Decompress {
private String _zipFile;
private String _location;
public Decompress(String zipFile, String location) {
_zipFile = zipFile;
_location = location;
_dirChecker("");
}
public void unzip() {
try {
FileInputStream fin = new FileInputStream(_zipFile);
ZipInputStream zin = new ZipInputStream(fin);
ZipEntry ze = null;
byte[] buffer = new byte[1024];
int length;
int i=0;
while ((ze = zin.getNextEntry()) != null) {
Log.v("t", ze.toString());
Log.v("Decompress", "Unzipping " + ze.getName());
if(ze.isDirectory()) {
Log.i("my","Comes to if");
_dirChecker(ze.getName());
}
else {
Log.i("my","Comes to else");
FileOutputStream fout = new FileOutputStream(_location + ze.getName());
while ((length = zin.read(buffer))>0) {
fout.write(buffer, 0, length);
}
zin.closeEntry();
fout.close();
}
}
zin.close();
Log.i("My tag","Success");
}catch(Exception e) {
Log.e("Decompress", "unzip", e);
}
}
private void _dirChecker(String dir) {
File f = new File(_location + dir);
if(!f.isDirectory()) {
Log.i("mytag", "Creating new folder");
f.mkdirs();
System.out.print("stp:"+f.getName());
}
}
}
*您是否試過*您的代碼? – bigstones 2011-03-02 11:50:32
這不是他的代碼 - 快速谷歌揭示了這一點:http://www.jondev.net/articles/Unzipping_Files_with_Android_%28Programmatically%29也許他應該寫自己的代碼,或開發能夠知道,如果一段代碼做什麼他期待。看來他不能做任何事情! – 2011-03-02 11:57:56