Compress

W
Document Sample
scope of work template
							Compress import java.io.*; import java.util.zip.*; public class Zip2Compress { public static void main(String args[]) { try { InputStreamReader inRead = new InputStreamReader(System.in); BufferedReader inReadBuff = new BufferedReader(inRead);

System.out.println("Enter the file to be zipped "); String fileName = inReadBuff.readLine(); System.out.println("Enter the zip format - ZIP or GZIP "); String zipFormat = inReadBuff.readLine(); Zip2Compress oZip = new Zip2Compress(); if(oZip.validateEntry(fileName, zipFormat)) { if(zipFormat.toLowerCase().trim().equals("zip")) { if(oZip.Zip(fileName)) { System.out.println("The file has been successfully zipped "); } else { System.out.println("The file has NOT zipped "); } } else if(zipFormat.toLowerCase().trim().equals("gzip")) { if(oZip.GZip(fileName)) { System.out.println("The file has been successfully zipped "); } else { System.out.println("The file has NOT zipped "); } } } else { System.out.println("Invalid Inputs. Try again "); } }catch(Exception e) { System.out.println(e.getMessage()); } } public boolean Zip(String fileName) { try { byte[] buffer = new byte[1024];

ZipOutputStream out = new ZipOutputStream(new FileOutputStream(fileName + ".zip")); FileInputStream in = new FileInputStream(fileName); out.putNextEntry(new ZipEntry(fileName)); int length; while ((length = in.read(buffer)) > 0) { out.write(buffer, 0, length); } out.closeEntry(); in.close(); out.close(); return true; } catch(Exception e) { System.out.println(e.getMessage()); return false; } } public boolean GZip(String fileName) { try { byte[] buffer = new byte[1024]; GZIPOutputStream out = new GZIPOutputStream(new FileOutputStream(fileName + ".gz")); FileInputStream in = new FileInputStream(fileName); int length; while ((length = in.read(buffer)) > 0) { out.write(buffer, 0, length); } in.close(); out.close(); return true; } catch(Exception e) { System.out.println(e.getMessage()); return false; } } public boolean validateEntry(String fileName, String format){ boolean validFile = false, validFormat = false;

try { File oF = new File(fileName); validFile = oF.isFile(); if((format.toLowerCase().trim().equals("zip")) || (format.toLowerCase().trim().equals("gzip"))) { validFormat = true; } else { System.out.println("The format passed is invalid "); } }catch(Exception e) { System.out.println(e.getMessage()); } if(validFile && validFormat) { return true; } else { return false; } } } Decompress import java.io.*; import java.util.zip.*;

public class Zip2Decompress {

public static void main(String[] args) { try { InputStreamReader inRead = new InputStreamReader(System.in); BufferedReader inReadBuff = new BufferedReader(inRead);

System.out.println("Enter the file to be un-zipped ");

String fileName = inReadBuff.readLine();

Zip2Decompress oUZip = new Zip2Decompress(); if(oUZip.validateEntry(fileName)) {

String outPutFileName = fileName.substring(0, fileName.lastIndexOf(".")); String zipFormat = fileName.substring(fileName.lastIndexOf(".") + 1, fileName.length()); if(zipFormat.toLowerCase().trim().equals("zip")) {

if(oUZip.unzip(fileName) == true) { System.out.println("The file has been successfully un-zipped "); } else { System.out.println("The file has NOT un-zipped "); } } else if(zipFormat.toLowerCase().trim().equals("gz")) { if(oUZip.gunzip(fileName, outPutFileName) == true) { System.out.println("The file has been successfully un-zipped "); } else { System.out.println("The file has NOT un-zipped "); } } } else {

System.out.println("The file is not valid "); }

}catch(Exception e) { System.out.println(e.getMessage()); }

}

public boolean unzip(String fileName) { try { ZipInputStream in = new ZipInputStream(new FileInputStream(fileName)); ZipEntry entry = in.getNextEntry();

String outFilename = entry.getName(); OutputStream out = new FileOutputStream(outFilename);

byte[] buffer = new byte[1024]; int len; while ((len = in.read(buffer)) > 0) { out.write(buffer, 0, len); }

out.close(); in.close();

return true; } catch(Exception e) { return false; } }

public boolean gunzip(String fileName, String outPutFileName) { try { GZIPInputStream in = new GZIPInputStream(new FileInputStream(fileName));

OutputStream out = new FileOutputStream(outPutFileName);

byte[] buffer = new byte[1024]; int len; while ((len = in.read(buffer)) > 0) { out.write(buffer, 0, len); }

out.close(); in.close();

return true; } catch(Exception e) { return false; } }

public boolean validateEntry(String fileName){ try { File oF = new File(fileName); return(oF.isFile()); }catch(Exception e) { System.out.println(e.getMessage()); return false; } }

}


						
Shared by: pptfiles
Related docs
Other docs by pptfiles