Advertisement
Guest User

Untitled

a guest
Mar 20th, 2019
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.25 KB | None | 0 0
  1. net.lingala.zip4j.core.ZipFile zipFile = new ZipFile("out.zip");
  2. ZipParameters parameters = new ZipParameters();
  3. parameters.setCompressionMethod(Zip4jConstants.COMP_DEFLATE);
  4. parameters.setCompressionLevel(Zip4jConstants.DEFLATE_LEVEL_NORMAL);
  5. zipFile.createZipFileFromFolder("path/to/source/dir", parameters, true, maximum size);
  6.  
  7. import java.io.FileInputStream;
  8. import java.io.FileNotFoundException;
  9. import java.io.FileOutputStream;
  10. import java.io.IOException;
  11. import java.util.zip.ZipEntry;
  12. import java.util.zip.ZipOutputStream;
  13.  
  14. public class QDE_ZIP {
  15.  
  16. public static String createZIP(String directoryPath, String zipFileName, String filesToZip) {
  17. try {
  18. final int BUFFER = 104857600; // 100MB
  19. final long MAX_ZIP_SIZE = 3221225472L; //3 GB
  20. long currentSize = 0;
  21. int zipSplitCount =0;
  22. String files[] = filesToZip.split(",");
  23. if (!directoryPath.endsWith("/")) {
  24. directoryPath = directoryPath + "/";
  25. }
  26. byte fileRAW[] = new byte[BUFFER];
  27. ZipOutputStream zipOut = new ZipOutputStream(new FileOutputStream(directoryPath + zipFileName.toUpperCase()));
  28. ZipEntry zipEntry;
  29. FileInputStream entryFile;
  30. for (String aFile : files) {
  31. zipEntry = new ZipEntry(aFile);
  32. if (currentSize >= MAX_ZIP_SIZE)
  33. {
  34. zipSplitCount ++;
  35. //zipOut.closeEntry();
  36. zipOut.close();
  37. zipOut = new ZipOutputStream(new FileOutputStream(directoryPath + zipFileName.toLowerCase().replace(".zip", "_"+zipSplitCount+".zip").toUpperCase()));
  38. currentSize = 0;
  39. }
  40. zipOut.putNextEntry(zipEntry);
  41. entryFile = new FileInputStream(directoryPath + aFile);
  42.  
  43. int count;
  44. while ((count = entryFile.read(fileRAW, 0, BUFFER)) != -1) {
  45. zipOut.write(fileRAW, 0, count);
  46.  
  47. //System.out.println("number of Bytes read = " + count);
  48. }
  49. entryFile.close();
  50. zipOut.closeEntry();
  51. currentSize += zipEntry.getCompressedSize();
  52. }
  53.  
  54. zipOut.close();
  55. //System.out.println(directory + " -" + zipFileName + " -Number of Files = " + files.length);
  56. } catch (FileNotFoundException e) {
  57. return "FileNotFoundException = " + e.getMessage();
  58. } catch (IOException e) {
  59. return "IOException = " + e.getMessage();
  60. } catch (Exception e) {
  61. return "Exception = " + e.getMessage();
  62. }
  63.  
  64. return "1";
  65. }
  66.  
  67. }
  68.  
  69. import java.util.zip.*;
  70. import java.io.*;
  71.  
  72.  
  73.  
  74. public class ChunkedZipTwo {
  75.  
  76. static final long MAX_LIMIT=10 * 1000 * 1024; //10MB limit - hopefully this
  77.  
  78.  
  79. public static void main(String[] args) throws Exception {
  80.  
  81.  
  82. String[] files = {"file1", "file2", "file3"};
  83. int i = 0;
  84. boolean needNewFile = false;
  85. long overallSize = 0;
  86. ZipOutputStream out = getOutputStream(i);
  87. byte[] buffer = new byte[1024];
  88.  
  89. for (String thisFileName: files) {
  90.  
  91.  
  92. if (overallSize > MAX_LIMIT) {
  93. out.close();
  94. i++;
  95. out = getOutputStream(i);
  96. overallSize=0;
  97. }
  98.  
  99. FileInputStream in = new FileInputStream(thisFileName);
  100. ZipEntry ze = new ZipEntry(thisFileName);
  101. out.putNextEntry(ze);
  102. int len;
  103. while ((len = in.read(buffer)) > 0) {
  104. out.write(buffer, 0, len);
  105. }
  106. out.closeEntry();
  107. in.close();
  108. overallSize+=ze.getCompressedSize();
  109.  
  110.  
  111.  
  112.  
  113. }
  114. out.close();
  115. }
  116.  
  117. public static ZipOutputStream getOutputStream(int i) throws IOException {
  118. ZipOutputStream out = new ZipOutputStream(new FileOutputStream("bigfile" + i +".zip"));
  119. out.setLevel(Deflater.DEFAULT_COMPRESSION);
  120. return out;
  121. }
  122. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement