Guest User

Untitled

a guest
Oct 15th, 2013
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.90 KB | None | 0 0
  1. Archive: t.zip
  2. Length Date Time Name
  3. -------- ---- ---- ----
  4. 9 04-11-09 09:11 t.txt
  5. 0 04-11-09 09:12 t/
  6. 15 04-11-09 09:12 t/t2.txt
  7. -------- -------
  8. 24 3 files
  9.  
  10. public class Test {
  11. public static void main(String[] args) {
  12. try {
  13. FileOutputStream f = new FileOutputStream("test.zip");
  14. ZipOutputStream zip = new ZipOutputStream(new BufferedOutputStream(f));
  15. zip.putNextEntry(new ZipEntry("xml/"));
  16. zip.putNextEntry(new ZipEntry("xml/xml"));
  17. zip.close();
  18. } catch(Exception e) {
  19. System.out.println(e.getMessage());
  20. }
  21. }
  22. }
  23.  
  24. public class ZipUsingJavaUtil
  25. {
  26. /*
  27. * Zip function zip all files and folders
  28. */
  29. @Override
  30. @SuppressWarnings("finally")
  31. public boolean zipFiles(String srcFolder, String destZipFile)
  32. {
  33. boolean result=false;
  34. try
  35. {
  36. System.out.println("Program Start zipping the given files");
  37. /*
  38. * send to the zip procedure
  39. */
  40. zipFolder(srcFolder,destZipFile);
  41. result=true;
  42. System.out.println("Given files are successfully zipped");
  43. }
  44. catch(Exception e)
  45. {
  46. System.out.println("Some Errors happned during the zip process");
  47. }
  48. finally
  49. {
  50. return result;
  51. }
  52. }
  53. /*
  54. * zip the folders
  55. */
  56. private void zipFolder(String srcFolder, String destZipFile) throws Exception
  57. {
  58. ZipOutputStream zip = null;
  59. FileOutputStream fileWriter = null;
  60. /*
  61. * create the output stream to zip file result
  62. */
  63. fileWriter = new FileOutputStream(destZipFile);
  64. zip = new ZipOutputStream(fileWriter);
  65. /*
  66. * add the folder to the zip
  67. */
  68. addFolderToZip("", srcFolder, zip);
  69. /*
  70. * close the zip objects
  71. */
  72. zip.flush();
  73. zip.close();
  74. }
  75. /*
  76. * recursively add files to the zip files
  77. */
  78. private void addFileToZip(String path, String srcFile, ZipOutputStream zip,boolean flag)throws Exception
  79. {
  80. /*
  81. * create the file object for inputs
  82. */
  83. File folder = new File(srcFile);
  84.  
  85. /*
  86. * if the folder is empty add empty folder to the Zip file
  87. */
  88. if (flag==true)
  89. {
  90. zip.putNextEntry(new ZipEntry(path + "/" +folder.getName() + "/"));
  91. }
  92. else
  93. { /*
  94. * if the current name is directory, recursively traverse it to get the files
  95. */
  96. if (folder.isDirectory() )
  97. {
  98. /*
  99. * if folder is not empty
  100. */
  101. addFolderToZip(path, srcFile, zip);
  102. }
  103. else
  104. {
  105. /*
  106. * write the file to the output
  107. */
  108. byte[] buf = new byte[1024];
  109. int len;
  110. FileInputStream in = new FileInputStream(srcFile);
  111. zip.putNextEntry(new ZipEntry(path + "/" + folder.getName()));
  112. while ((len = in.read(buf)) > 0)
  113. {
  114. /*
  115. * Write the Result
  116. */
  117. zip.write(buf, 0, len);
  118. }
  119. }
  120. }
  121. }
  122. /*
  123. * add folder to the zip file
  124. */
  125. private void addFolderToZip(String path, String srcFolder, ZipOutputStream zip)
  126. throws Exception
  127. {
  128. File folder = new File(srcFolder);
  129.  
  130. /*
  131. * check the empty folder
  132. */
  133. if (folder.list().length == 0)
  134. {
  135. System.out.println(folder.getName());
  136. addFileToZip(path , srcFolder, zip,true);
  137. }
  138. else
  139. {
  140. /*
  141. * list the files in the folder
  142. */
  143. for (String fileName : folder.list())
  144. {
  145. if (path.equals(""))
  146. {
  147. addFileToZip(folder.getName(), srcFolder + "/" + fileName, zip,false);
  148. }
  149. else
  150. {
  151. addFileToZip(path + "/" + folder.getName(), srcFolder + "/" + fileName, zip,false);
  152. }
  153. }
  154. }
  155. }
Advertisement
Add Comment
Please, Sign In to add comment