我究竟应该在这段代码中更改什么才能设置保存文件的路径?
What exactly should I change in this snip of code to have ability set a path to a save file?
请帮帮我
我真的不明白如何更改这段代码才能设置保存文件的路径。
我需要解压一个文件。我想给方法 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
它应该在 AvatarModelDir
中创建一个 Default name dirictory
并解压当前的 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);
在Decompress Constructor里面设置你想要设置的目录名。
dirChecker("MyUnzip");
请帮帮我
我真的不明白如何更改这段代码才能设置保存文件的路径。
我需要解压一个文件。我想给方法 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
它应该在 AvatarModelDir
中创建一个 Default name dirictory
并解压当前的 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);
在Decompress Constructor里面设置你想要设置的目录名。
dirChecker("MyUnzip");