Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.BufferedInputStream;
- import java.io.BufferedOutputStream;
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.FileOutputStream;
- import java.util.zip.ZipEntry;
- import java.util.zip.ZipOutputStream;
- //import java.io.IOException;
- //import java.io.InputStreamReader;
- //import java.io.BufferedReader;
- public class Algorithms
- {
- static final int BUFFER = 2048;
- //this algorithm is meant to go recursively through a directory
- //and compress each file individually. if a file is already
- //compressed with some compression method, it will be ignored
- void RECZip (String location)
- {
- File directory = new File(location);
- //check to see if the selected location exists
- if (directory.exists()==true && directory.canRead()==true)
- {
- //if it's a directory, pass through the contents and compress each file,
- //call RECZip for each directory found
- for (String passedstring : directory.list())
- {
- if(passedstring.endsWith(".zip")==false &&
- passedstring.endsWith(".rar")==false
- && passedstring.endsWith(".7z")==false
- && passedstring.endsWith(".chm"))
- {
- //create a temporary file for the dir/file that is currently in for loop
- File temp = new File(directory.getAbsolutePath(), passedstring);
- //if it's a directory, call RECZip
- if (temp.isDirectory())
- RECZip(temp.getAbsolutePath());
- //if it's a file, compress it
- else if (temp.isFile())
- {
- try
- {
- //compression method starts here WILL COMPRESS TO ZIP. WANT THIS TO BE REPLACED WITH 7z COMPRESSION
- FileInputStream fi = new FileInputStream(temp.getAbsolutePath());
- BufferedInputStream FileToCompress = new BufferedInputStream(fi, BUFFER);
- FileOutputStream dest = new FileOutputStream(location + "/" + passedstring + ".zip");
- ZipOutputStream out = new ZipOutputStream(new BufferedOutputStream(dest));
- ZipEntry entry = new ZipEntry(passedstring);
- out.setMethod(ZipOutputStream.DEFLATED);
- out.putNextEntry(entry);
- byte data[] = new byte[BUFFER];
- int count;
- while((count = FileToCompress.read(data, 0, BUFFER)) != -1)
- {
- out.write(data, 0, count);
- }
- //compression method ends here
- FileToCompress.close(); //close the file being compressed
- out.close(); //close the zip stream
- temp.delete();
- }
- catch (Exception e)
- {
- e.printStackTrace();
- }
- }
- }
- } //end for
- } //end directory.exists(), canRead(), isDirectory()
- else
- {
- if (directory.exists()==false)
- {
- //tell user it doesn't exist
- }
- if (directory.canRead()==false)
- {
- //tell user it can't be read
- }
- }
- } //end method RECZip
- } // end class algorithms
- /*The following is a possibility for compressing the files to 7z using calls to the terminal
- *
- * Place the lines below where the set of calls are to create the zip
- * CommandProcess("7z a " + passedstring + ".7z " + passedstring, new File(temp.getCanonicalPath()));
- a static process was used last time - static Process tocommand;
- void CommandProcess(String command, String[] envir, File workingdirectory)
- {
- try
- {
- // run the Unix "7z a " command
- // using the Runtime exec method:
- String s = null;
- tocommand = Runtime.getRuntime().exec(command, envir, workingdirectory);
- CommandProcess("cd " + temp.getAbsolutePath());
- BufferedReader stdInput = new BufferedReader(new
- InputStreamReader(tocommand.getInputStream()));
- BufferedReader stdError = new BufferedReader(new
- InputStreamReader(tocommand.getErrorStream()));
- // read the output from the command
- System.out.println("Here is the standard output of the command:\n");
- while ((s = stdInput.readLine()) != null) {
- System.out.println(s);
- }
- // read any errors from the attempted command
- System.out.println("Here is the standard error of the command (if any):\n");
- while ((s = stdError.readLine()) != null) {
- System.out.println(s);
- }
- System.exit(0);
- }
- catch (IOException e) {
- System.out.println("exception happened - here's what I know: ");
- e.printStackTrace();
- System.exit(-1);
- }
- } //end possible command to terminal
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement