Advertisement
Guest User

Untitled

a guest
Jun 24th, 2019
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.78 KB | None | 0 0
  1. adb.exe -e shell "run-as com.example.ivan.fgc cat /data/data/com.example.ivan.fgc/files/archive.zip" > archive.zip
  2.  
  3. /**
  4. * Упаковывает все файлы директории files и дополняет ими архив archive.zip
  5. *
  6. * @param context Контекст
  7. * @throws IOException Исключение записи/чтения
  8. */
  9. @SuppressWarnings("ResultOfMethodCallIgnored")
  10. public void packIntoArchive(Context context) throws IOException {
  11. ArrayList<File> files = getFiles(new File(context.getFilesDir().getPath()));
  12. if (!files.isEmpty()) {
  13. FileOutputStream fos = new FileOutputStream(context.getFilesDir().getPath() + "/archive.zip", true);
  14. ZipOutputStream zipOut = new ZipOutputStream(fos);
  15.  
  16. for (File file : files) {
  17. FileInputStream fis = new FileInputStream(file);
  18. ZipEntry zipEntry = new ZipEntry(file.getPath().substring(context.getFilesDir().getPath().length() + 1));
  19. zipOut.putNextEntry(zipEntry);
  20. byte[] bytes = new byte[1024];
  21. int length;
  22. while ((length = fis.read(bytes)) > 0) {
  23. zipOut.write(bytes, 0, length);
  24. }
  25. zipOut.closeEntry();
  26. fis.close();
  27. file.delete();
  28. }
  29.  
  30. zipOut.close();
  31. fos.close();
  32.  
  33. removeFilesNDirs(new File(context.getFilesDir().getPath()));
  34. }
  35. }
  36.  
  37. /**
  38. * Удалить всё кроме архивов из папки
  39. *
  40. * @param dir Директория
  41. */
  42. @SuppressWarnings("ResultOfMethodCallIgnored")
  43. private void removeFilesNDirs(File dir) {
  44. for (File file : dir.listFiles()) {
  45. if (file.isDirectory()) {
  46. removeFilesNDirs(file);
  47. }
  48. if (!file.getName().contains(".zip")) {
  49. file.delete();
  50. }
  51. }
  52. }
  53.  
  54. /**
  55. * Получить все файлы из директории (за исключением архивов)
  56. *
  57. * @param dir Папка
  58. * @return Массив файлов
  59. */
  60. private ArrayList<File> getFiles(File dir) {
  61. ArrayList<File> filesNDirs = new ArrayList<>(Arrays.asList(dir.listFiles()));
  62. ArrayList<File> files = new ArrayList<>();
  63. Iterator<File> iterator = filesNDirs.iterator();
  64. while (iterator.hasNext()) {
  65. File file = iterator.next();
  66. if (file.isDirectory()) {
  67. files.addAll(getFiles(file));
  68. iterator.remove();
  69. } else {
  70. if (!file.getName().contains(".zip")) {
  71. files.add(file);
  72. }
  73. }
  74. }
  75. return files;
  76. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement