请帮帮我
我真的不明白如何更改此代码片段以具有设置文件保存路径的能力。
我需要解压缩文件。我想给方法2自变量:第一个是zip文件的路径,第二个是您要存储解压缩文件的路径。就是这样...但这让我发疯了))
我有代码
public class Decompress {
private String zipFile;
private String location;
private final String MY_LOG = "Decompress";
public Decompress(String zipFile, String location) {
this.zipFile = zipFile;
this.location = location;
dirChecker("");
}
public void unzip() {
try {
FileInputStream fin = new FileInputStream(zipFile);
ZipInputStream zis = new ZipInputStream(fin);
ZipEntry ze;
while ((ze = zis.getNextEntry()) != null) {
Log.e(MY_LOG, "Unzipping " + ze.getName());
if (ze.isDirectory()) {
dirChecker(ze.getName());
} else {
write(zis, new FileOutputStream(location + ze.getName()));
zis.closeEntry();
}
}
zis.close();
} catch (Exception e) {
Log.e(MY_LOG, "unzip", e);
}
}
private void write(InputStream in, OutputStream out) throws IOException {
byte[] buffer = new byte[1024];
int len;
while ((len = in.read(buffer)) >= 0) {
out.write(buffer, 0, len);
}
out.close();
}
private void dirChecker(String dir) {
File f = new File(location + dir);
if (!f.isDirectory()) {
f.mkdirs();
}
}
我在构造函数中设置
zipFile = /storage/emulated/0/Android/data/com.example.android.camera2basic.demo/files/ttt@gmail.com/AvatarModelDir/new.zip
我需要将此
new.zip
解压缩到当前目录AvatarModelDir
中。据此,我设定...location = /storage/emulated/0/Android/data/com.example.android.camera2basic.demo/files/ttt@gmail.com/AvatarModelDir
我希望解压缩文件的新路径如下所示
/storage/emulated/0/Android/data/com.example.android.camera2basic.demo/files/ttt@gmail.com/AvatarModelDir/MyUnzip/Anna.dae
但是,它创建了这个目录
/storage/emulated/0/Android/data/com.example.android.camera2basic.demo/files/ttt@gmail.com/AvatarModelDirAnna/Anna.dae
为什么将
Anna
应用于AvatarModelDir
以及为什么在级别ttt@gmail.com
而不是AvatarModelDir
上创建目录我只需要将路径设置为
zip
文件,并将其解压缩到的路径(提取目录)我希望例如设置路径解压缩
/storage/emulated/0/Android/data/com.example.android.camera2basic.demo/files/ttt@gmail.com/AvatarModelDir
它应该在
Default name dirictory
内创建一个AvatarModelDir
并解压缩当前的zip文件/storage/emulated/0/Android/data/com.example.android.camera2basic.demo/files/ttt@gmail.com/AvatarModelDir/DefaultNameDirectory/...
最佳答案
内部dirChecker方法
File f = new File(location + dir);
location和dir是串联的,而不是为新目录创建新路径。
应该像
String path = location + (!dir.isEmpty()?"/"+dir:"");
File f = new File(path);
在内部解压缩构造函数中,设置要设置的目录名称。
dirChecker("MyUnzip");