Advertisement
Guest User

Untitled

a guest
Dec 17th, 2009
165
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.56 KB | None | 0 0
  1. import java.io.BufferedInputStream;
  2. import java.io.BufferedOutputStream;
  3. import java.io.File;
  4. import java.io.FileInputStream;
  5. import java.io.FileOutputStream;
  6. import java.util.zip.ZipEntry;
  7. import java.util.zip.ZipOutputStream;
  8. //import java.io.IOException;
  9. //import java.io.InputStreamReader;
  10. //import java.io.BufferedReader;
  11.  
  12. public class Algorithms
  13. {
  14. static final int BUFFER = 2048;
  15. //this algorithm is meant to go recursively through a directory
  16. //and compress each file individually. if a file is already
  17. //compressed with some compression method, it will be ignored
  18. void RECZip (String location)
  19. {
  20. File directory = new File(location);
  21. //check to see if the selected location exists
  22. if (directory.exists()==true && directory.canRead()==true)
  23. {
  24. //if it's a directory, pass through the contents and compress each file,
  25. //call RECZip for each directory found
  26. for (String passedstring : directory.list())
  27. {
  28. if(passedstring.endsWith(".zip")==false &&
  29. passedstring.endsWith(".rar")==false
  30. && passedstring.endsWith(".7z")==false
  31. && passedstring.endsWith(".chm"))
  32. {
  33. //create a temporary file for the dir/file that is currently in for loop
  34. File temp = new File(directory.getAbsolutePath(), passedstring);
  35. //if it's a directory, call RECZip
  36. if (temp.isDirectory())
  37. RECZip(temp.getAbsolutePath());
  38.  
  39. //if it's a file, compress it
  40. else if (temp.isFile())
  41. {
  42. try
  43. {
  44. //compression method starts here WILL COMPRESS TO ZIP. WANT THIS TO BE REPLACED WITH 7z COMPRESSION
  45. FileInputStream fi = new FileInputStream(temp.getAbsolutePath());
  46. BufferedInputStream FileToCompress = new BufferedInputStream(fi, BUFFER);
  47.  
  48. FileOutputStream dest = new FileOutputStream(location + "/" + passedstring + ".zip");
  49. ZipOutputStream out = new ZipOutputStream(new BufferedOutputStream(dest));
  50. ZipEntry entry = new ZipEntry(passedstring);
  51. out.setMethod(ZipOutputStream.DEFLATED);
  52. out.putNextEntry(entry);
  53. byte data[] = new byte[BUFFER];
  54. int count;
  55. while((count = FileToCompress.read(data, 0, BUFFER)) != -1)
  56. {
  57. out.write(data, 0, count);
  58. }
  59. //compression method ends here
  60. FileToCompress.close(); //close the file being compressed
  61. out.close(); //close the zip stream
  62. temp.delete();
  63. }
  64. catch (Exception e)
  65. {
  66. e.printStackTrace();
  67. }
  68. }
  69. }
  70. } //end for
  71. } //end directory.exists(), canRead(), isDirectory()
  72. else
  73. {
  74. if (directory.exists()==false)
  75. {
  76. //tell user it doesn't exist
  77. }
  78. if (directory.canRead()==false)
  79. {
  80. //tell user it can't be read
  81. }
  82. }
  83. } //end method RECZip
  84.  
  85.  
  86. } // end class algorithms
  87.  
  88.  
  89. /*The following is a possibility for compressing the files to 7z using calls to the terminal
  90. *
  91. * Place the lines below where the set of calls are to create the zip
  92. * CommandProcess("7z a " + passedstring + ".7z " + passedstring, new File(temp.getCanonicalPath()));
  93.  
  94. a static process was used last time - static Process tocommand;
  95.  
  96. void CommandProcess(String command, String[] envir, File workingdirectory)
  97. {
  98. try
  99. {
  100. // run the Unix "7z a " command
  101. // using the Runtime exec method:
  102. String s = null;
  103.  
  104. tocommand = Runtime.getRuntime().exec(command, envir, workingdirectory);
  105.  
  106. CommandProcess("cd " + temp.getAbsolutePath());
  107.  
  108. BufferedReader stdInput = new BufferedReader(new
  109. InputStreamReader(tocommand.getInputStream()));
  110.  
  111. BufferedReader stdError = new BufferedReader(new
  112. InputStreamReader(tocommand.getErrorStream()));
  113.  
  114. // read the output from the command
  115. System.out.println("Here is the standard output of the command:\n");
  116. while ((s = stdInput.readLine()) != null) {
  117. System.out.println(s);
  118. }
  119.  
  120. // read any errors from the attempted command
  121. System.out.println("Here is the standard error of the command (if any):\n");
  122. while ((s = stdError.readLine()) != null) {
  123. System.out.println(s);
  124. }
  125.  
  126. System.exit(0);
  127. }
  128. catch (IOException e) {
  129. System.out.println("exception happened - here's what I know: ");
  130. e.printStackTrace();
  131. System.exit(-1);
  132. }
  133. } //end possible command to terminal
  134.  
  135. */
  136.  
  137.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement