package com.megatim.module.compression.impl; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.util.zip.GZIPInputStream; import java.util.zip.GZIPOutputStream; import net.lingala.zip4j.exception.ZipException; import com.megatim.module.compression.ifaces.CompressionFace; public class JavaZipGZIPImpl implements CompressionFace { public static int sChunk = 8192; public File zipFile(File fichierAZipper) throws IOException { // TODO Auto-generated method stub // create output stream String zipname = removeExtension(fichierAZipper.getPath()) + ".gz"; GZIPOutputStream zipout; try { FileOutputStream out = new FileOutputStream(zipname); zipout = new GZIPOutputStream(out); } catch (IOException e) { System.out.println("Couldn't create " + zipname + "."); return null; } byte[] buffer = new byte[sChunk]; // compress the file try { FileInputStream in = new FileInputStream(removeExtension(fichierAZipper.getPath())); int length; while ((length = in.read(buffer, 0, sChunk)) != -1) { zipout.write(buffer, 0, length); } in.close(); } catch (IOException e) { System.out.println("Couldn't compress " + removeExtension(fichierAZipper.getPath()) + "."); } try { zipout.close(); } catch (IOException e) { } return new File(removeExtension(fichierAZipper.getPath()) + ".gz"); } public File zipFile(File fichierAZipper, File repertoireDestination) throws IOException { return null; } public File zipRepository(File repertoireAZipper) throws IOException { // TODO Auto-generated method stub // create output stream String zipname = repertoireAZipper.getPath() + ".gz"; GZIPOutputStream zipout; try { FileOutputStream out = new FileOutputStream(zipname); zipout = new GZIPOutputStream(out); } catch (IOException e) { System.out.println("Couldn't create " + zipname + "."); return null; } byte[] buffer = new byte[sChunk]; // compress the file try { FileInputStream in = new FileInputStream(repertoireAZipper.getPath()); int length; while ((length = in.read(buffer, 0, sChunk)) != -1) { zipout.write(buffer, 0, length); } in.close(); } catch (IOException e) { System.out.println("Couldn't compress " + repertoireAZipper.getPath() + "."); } try { zipout.close(); } catch (IOException e) { } return new File(repertoireAZipper.getPath() + ".gz"); } public File zipRepository(File repertoireAZipper, File repertoireDestination) throws IOException { return null; } public void unZipFile(File fichierZipper, File repertoireSorti) throws IOException { // TODO Auto-generated method stub // create input stream String zipname, source; if (fichierZipper.getPath().endsWith(".gz")) { zipname = fichierZipper.getPath(); source = repertoireSorti.getPath(); } else { zipname = fichierZipper.getPath() + ".gz"; source = repertoireSorti.getPath(); } GZIPInputStream zipin = null; try { FileInputStream in = new FileInputStream(zipname); zipin = new GZIPInputStream(in); } catch (IOException e) { System.out.println("Couldn't open " + zipname + "."); return; } byte[] buffer = new byte[sChunk]; // decompress the file try { FileOutputStream out = new FileOutputStream(source); int length; while ((length = zipin.read(buffer, 0, sChunk)) != -1) { out.write(buffer, 0, length); } out.close(); } catch (IOException e) { System.out.println("Couldn't decompress " + fichierZipper.getName() + "."); } try { zipin.close(); } catch (IOException e) { } } public void unZipRepository(File repertoireZipper, File repertoireSorti) throws IOException { // TODO Auto-generated method stub // create input stream String zipname, source; if (repertoireZipper.getPath().endsWith(".gz")) { zipname = repertoireZipper.getPath(); source = repertoireSorti.getPath(); } else { zipname = repertoireZipper.getPath() + ".gz"; source = repertoireSorti.getPath(); } GZIPInputStream zipin = null; try { FileInputStream in = new FileInputStream(zipname); zipin = new GZIPInputStream(in); } catch (IOException e) { System.out.println("Couldn't open " + zipname + "."); return; } byte[] buffer = new byte[sChunk]; // decompress the file try { FileOutputStream out = new FileOutputStream(source); int length; while ((length = zipin.read(buffer, 0, sChunk)) != -1) { out.write(buffer, 0, length); } out.close(); } catch (IOException e) { System.out.println("Couldn't decompress " + repertoireZipper.getName() + "."); } try { zipin.close(); } catch (IOException e) { } } public void addFile(File fichier, File zipFile) throws IOException, Exception { // TODO Auto-generated method stub // create output stream String zipname = zipFile.getPath(); GZIPOutputStream zipout = null; try { FileOutputStream out = new FileOutputStream(zipname); zipout = new GZIPOutputStream(out); } catch (IOException e) { System.out.println("Couldn't create " + zipname + "."); } byte[] buffer = new byte[sChunk]; // compress the file try { FileInputStream in = new FileInputStream(removeExtension(fichier.getPath())); int length; while ((length = in.read(buffer, 0, sChunk)) != -1) { zipout.write(buffer, 0, length); } in.close(); } catch (IOException e) { System.out.println("Couldn't compress " + fichier + "."); } try { zipout.close(); } catch (IOException e) { } } public void addRepository(File repository, File zipFile) throws FileNotFoundException, Exception { // TODO Auto-generated method stub // create output stream String zipname = zipFile.getPath(); GZIPOutputStream zipout = null; try { FileOutputStream out = new FileOutputStream(zipname); zipout = new GZIPOutputStream(out); } catch (IOException e) { System.out.println("Couldn't create " + zipname + "."); } byte[] buffer = new byte[sChunk]; // compress the file try { FileInputStream in = new FileInputStream(repository.getPath()); int length; while ((length = in.read(buffer, 0, sChunk)) != -1) { zipout.write(buffer, 0, length); } in.close(); } catch (IOException e) { System.out.println("Couldn't compress " + repository.getName() + "."); } try { zipout.close(); } catch (IOException e) { } } public File zipFile(File fichierAZipper, String password) throws ZipException, Exception { // TODO Auto-generated method stub return null; } public File zipRepository(File repertoireAZipper, String password) throws Exception { // TODO Auto-generated method stub return null; } public void unZipFile(File fichierZipper, File repertoireSorti, String password) throws Exception { // TODO Auto-generated method stub } public void unZipRepository(File repertoireZipper, File repertoireSorti, String password) throws ZipException, Exception { // TODO Auto-generated method stub } public void addFile(File fichier, File zipFile, String password) throws Exception { // TODO Auto-generated method stub } public void addRepository(File repository, File zipFile, String password) throws Exception { // TODO Auto-generated method stub } // Remove extension before zip file public static String removeExtension(String s) { String separator = System.getProperty("file.separator"); String filename; // Remove the path upto the filename. /* * int lastSeparatorIndex = s.lastIndexOf(separator); if (lastSeparatorIndex == * -1) { filename = s; } else { filename = s.substring(lastSeparatorIndex + 1); * } */ // Remove the extension. int extensionIndex = s.lastIndexOf("."); if (extensionIndex == -1) { return s; } return s.substring(0, extensionIndex); } public File zipFile(File fichierAZipper,File repertoireDestination, String password) throws IOException { return null; } }