101 lines
2.9 KiB
Java
101 lines
2.9 KiB
Java
package org.torproject.android.ui.hiddenservices.backup;
|
|
|
|
|
|
import android.support.annotation.NonNull;
|
|
import android.support.annotation.Nullable;
|
|
|
|
import java.io.BufferedInputStream;
|
|
import java.io.BufferedOutputStream;
|
|
import java.io.File;
|
|
import java.io.FileInputStream;
|
|
import java.io.FileOutputStream;
|
|
import java.io.IOException;
|
|
import java.io.InputStream;
|
|
import java.util.zip.ZipEntry;
|
|
import java.util.zip.ZipInputStream;
|
|
import java.util.zip.ZipOutputStream;
|
|
|
|
public class ZipIt {
|
|
private static final int BUFFER = 2048;
|
|
|
|
private String[] _files;
|
|
private String _zipFile;
|
|
|
|
public ZipIt(@Nullable String[] files, @NonNull String zipFile) {
|
|
_files = files;
|
|
_zipFile = zipFile;
|
|
}
|
|
|
|
public boolean zip() {
|
|
try {
|
|
BufferedInputStream origin;
|
|
FileOutputStream dest = new FileOutputStream(_zipFile);
|
|
|
|
ZipOutputStream out = new ZipOutputStream(new BufferedOutputStream(dest));
|
|
|
|
byte data[] = new byte[BUFFER];
|
|
|
|
for (String _file : _files) {
|
|
FileInputStream fi = new FileInputStream(_file);
|
|
origin = new BufferedInputStream(fi, BUFFER);
|
|
ZipEntry entry = new ZipEntry(_file.substring(_file.lastIndexOf("/") + 1));
|
|
out.putNextEntry(entry);
|
|
int count;
|
|
while ((count = origin.read(data, 0, BUFFER)) != -1) {
|
|
out.write(data, 0, count);
|
|
}
|
|
origin.close();
|
|
}
|
|
|
|
out.close();
|
|
} catch (Exception e) {
|
|
return false;
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
public boolean unzip(String output_path) {
|
|
InputStream is;
|
|
ZipInputStream zis;
|
|
|
|
try {
|
|
String filename;
|
|
is = new FileInputStream(_zipFile);
|
|
zis = new ZipInputStream(new BufferedInputStream(is));
|
|
ZipEntry ze;
|
|
byte[] buffer = new byte[1024];
|
|
int count;
|
|
|
|
while ((ze = zis.getNextEntry()) != null) {
|
|
// zapis do souboru
|
|
filename = ze.getName();
|
|
|
|
// Need to create directories if not exists, or
|
|
// it will generate an Exception...
|
|
if (ze.isDirectory()) {
|
|
File fmd = new File(output_path + "/" + filename);
|
|
fmd.mkdirs();
|
|
continue;
|
|
}
|
|
|
|
FileOutputStream fout = new FileOutputStream(output_path + "/" + filename);
|
|
|
|
// cteni zipu a zapis
|
|
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;
|
|
}
|
|
} |