我正在unziping一個基於官方文檔和一些例子的文件。我目前的實現將該文件解壓縮到zip文件所在的同一目錄中。我想解壓到設備中的特定目錄。我怎樣才能做到這一點? ZipInputStream允許這個功能,還是我必須解壓縮,然後將文件移動到所需的文件夾?如何解壓縮文件在Android中執行特定的文件夾?
這是我的代碼:
public static boolean unpackZip(String path, String zipname) {
InputStream is;
ZipInputStream zis;
try {
String filename;
is = new FileInputStream(path + zipname);
zis = new ZipInputStream(new BufferedInputStream(is));
ZipEntry ze;
byte[] buffer = new byte[1024];
int count;
while ((ze = zis.getNextEntry()) != null) {
filename = ze.getName();
if (ze.isDirectory()) {
File fmd = new File(path + filename);
fmd.mkdirs();
continue;
}
FileOutputStream fout = new FileOutputStream(path + filename);
while ((count = zis.read(buffer)) != -1) {
fout.write(buffer, 0, count);
}
fout.close();
zis.closeEntry();
}
zis.close();
} catch (IOException e) {
e.printStackTrace();
return false;
}
return true;
}