Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- adb.exe -e shell "run-as com.example.ivan.fgc cat /data/data/com.example.ivan.fgc/files/archive.zip" > archive.zip
- /**
- * Упаковывает все файлы директории files и дополняет ими архив archive.zip
- *
- * @param context Контекст
- * @throws IOException Исключение записи/чтения
- */
- @SuppressWarnings("ResultOfMethodCallIgnored")
- public void packIntoArchive(Context context) throws IOException {
- ArrayList<File> files = getFiles(new File(context.getFilesDir().getPath()));
- if (!files.isEmpty()) {
- FileOutputStream fos = new FileOutputStream(context.getFilesDir().getPath() + "/archive.zip", true);
- ZipOutputStream zipOut = new ZipOutputStream(fos);
- for (File file : files) {
- FileInputStream fis = new FileInputStream(file);
- ZipEntry zipEntry = new ZipEntry(file.getPath().substring(context.getFilesDir().getPath().length() + 1));
- zipOut.putNextEntry(zipEntry);
- byte[] bytes = new byte[1024];
- int length;
- while ((length = fis.read(bytes)) > 0) {
- zipOut.write(bytes, 0, length);
- }
- zipOut.closeEntry();
- fis.close();
- file.delete();
- }
- zipOut.close();
- fos.close();
- removeFilesNDirs(new File(context.getFilesDir().getPath()));
- }
- }
- /**
- * Удалить всё кроме архивов из папки
- *
- * @param dir Директория
- */
- @SuppressWarnings("ResultOfMethodCallIgnored")
- private void removeFilesNDirs(File dir) {
- for (File file : dir.listFiles()) {
- if (file.isDirectory()) {
- removeFilesNDirs(file);
- }
- if (!file.getName().contains(".zip")) {
- file.delete();
- }
- }
- }
- /**
- * Получить все файлы из директории (за исключением архивов)
- *
- * @param dir Папка
- * @return Массив файлов
- */
- private ArrayList<File> getFiles(File dir) {
- ArrayList<File> filesNDirs = new ArrayList<>(Arrays.asList(dir.listFiles()));
- ArrayList<File> files = new ArrayList<>();
- Iterator<File> iterator = filesNDirs.iterator();
- while (iterator.hasNext()) {
- File file = iterator.next();
- if (file.isDirectory()) {
- files.addAll(getFiles(file));
- iterator.remove();
- } else {
- if (!file.getName().contains(".zip")) {
- files.add(file);
- }
- }
- }
- return files;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement