Help me please
I really can't understand how to change this snip of code to have ability set a path to save file.
I need unzip a file. I want give the method 2 arguments: the first is the path to the zip file and the second is the path where you want to store the unzipped file. That's it... But it's driving me crazy))
I have the code
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();
}
}
I set this in the constructor
zipFile = /storage/emulated/0/Android/data/com.example.android.camera2basic.demo/files/[email protected]/AvatarModelDir/new.zip
and I need to unzip this new.zip
inside the current directory AvatarModelDir
. According to this, I set...
location = /storage/emulated/0/Android/data/com.example.android.camera2basic.demo/files/[email protected]/AvatarModelDir
And I expect that the new path for unzip files will be like this
/storage/emulated/0/Android/data/com.example.android.camera2basic.demo/files/[email protected]/AvatarModelDir/MyUnzip/Anna.dae
but instead, it creates this directory
/storage/emulated/0/Android/data/com.example.android.camera2basic.demo/files/[email protected]/AvatarModelDirAnna/Anna.dae
Why it appent Anna
to AvatarModelDir
and why it create dir on level [email protected]
instead of AvatarModelDir
I just need set the path to the zip
file and the path where I want to unzip it in to (Extract Directory)
I expect the set path to unzip for example
/storage/emulated/0/Android/data/com.example.android.camera2basic.demo/files/[email protected]/AvatarModelDir
and it should create a Default name dirictory
inside AvatarModelDir
and unzip current zip file
/storage/emulated/0/Android/data/com.example.android.camera2basic.demo/files/[email protected]/AvatarModelDir/DefaultNameDirectory/...
Inside dirChecker Method
File f = new File(location + dir);
location and dir are concatenating instead of creating a new path for the new directory.
It should be like
String path = location + (!dir.isEmpty()?"/"+dir:"");
File f = new File(path);
Inside Decompress Constructor set the dir name that you want to be set.
dirChecker("MyUnzip");