Advertisement
Guest User

Untitled

a guest
Oct 31st, 2014
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.56 KB | None | 0 0
  1. /*
  2. *
  3. * Zips a file at a location and places the resulting zip file at the toLocation
  4. * Example: zipFileAtPath("downloads/myfolder", "downloads/myFolder.zip");
  5. */
  6.  
  7. public boolean zipFileAtPath(String sourcePath, String toLocation) {
  8. // ArrayList<String> contentList = new ArrayList<String>();
  9. final int BUFFER = 2048;
  10.  
  11.  
  12. File sourceFile = new File(sourcePath);
  13. try {
  14. BufferedInputStream origin = null;
  15. FileOutputStream dest = new FileOutputStream(toLocation);
  16. ZipOutputStream out = new ZipOutputStream(new BufferedOutputStream(
  17. dest));
  18. if (sourceFile.isDirectory()) {
  19. zipSubFolder(out, sourceFile, sourceFile.getParent().length());
  20. } else {
  21. byte data[] = new byte[BUFFER];
  22. FileInputStream fi = new FileInputStream(sourcePath);
  23. origin = new BufferedInputStream(fi, BUFFER);
  24. ZipEntry entry = new ZipEntry(getLastPathComponent(sourcePath));
  25. out.putNextEntry(entry);
  26. int count;
  27. while ((count = origin.read(data, 0, BUFFER)) != -1) {
  28. out.write(data, 0, count);
  29. }
  30. }
  31. out.close();
  32. } catch (Exception e) {
  33. e.printStackTrace();
  34. return false;
  35. }
  36. return true;
  37. }
  38.  
  39. /*
  40. *
  41. * Zips a subfolder
  42. *
  43. */
  44.  
  45. private void zipSubFolder(ZipOutputStream out, File folder,
  46. int basePathLength) throws IOException {
  47.  
  48. final int BUFFER = 2048;
  49.  
  50. File[] fileList = folder.listFiles();
  51. BufferedInputStream origin = null;
  52. for (File file : fileList) {
  53. if (file.isDirectory()) {
  54. zipSubFolder(out, file, basePathLength);
  55. } else {
  56. byte data[] = new byte[BUFFER];
  57. String unmodifiedFilePath = file.getPath();
  58. String relativePath = unmodifiedFilePath
  59. .substring(basePathLength);
  60. Log.i("ZIP SUBFOLDER", "Relative Path : " + relativePath);
  61. FileInputStream fi = new FileInputStream(unmodifiedFilePath);
  62. origin = new BufferedInputStream(fi, BUFFER);
  63. ZipEntry entry = new ZipEntry(relativePath);
  64. out.putNextEntry(entry);
  65. int count;
  66. while ((count = origin.read(data, 0, BUFFER)) != -1) {
  67. out.write(data, 0, count);
  68. }
  69. origin.close();
  70. }
  71. }
  72. }
  73.  
  74. /*
  75. * gets the last path component
  76. *
  77. * Example: getLastPathComponent("downloads/example/fileToZip");
  78. * Result: "fileToZip"
  79. */
  80. public String getLastPathComponent(String filePath) {
  81. String[] segments = filePath.split("/");
  82. String lastPathComponent = segments[segments.length - 1];
  83. return lastPathComponent;
  84. }
  85.  
  86. private static void zipFolder(String inputFolderPath, String outZipPath) {
  87. try {
  88. FileOutputStream fos = new FileOutputStream(outZip);
  89. ZipOutputStream zos = new ZipOutputStream(fos);
  90. File srcFile = new File(inputFolder);
  91. File[] files = srcFile.listFiles();
  92. Log.d("", "Zip directory: " + srcFile.getName());
  93. for (int i = 0; i < files.length; i++) {
  94. Log.d("", "Adding file: " + files[i].getName());
  95. byte[] buffer = new byte[1024];
  96. FileInputStream fis = new FileInputStream(files[i]);
  97. zos.putNextEntry(new ZipEntry(files[i].getName()));
  98. int length;
  99. while ((length = fis.read(buffer)) > 0) {
  100. zos.write(buffer, 0, length);
  101. }
  102. zos.closeEntry();
  103. fis.close();
  104. }
  105. zos.close();
  106. } catch (IOException ioe) {
  107. Log.e("", ioe.getMessage());
  108. }
  109. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement