Created
March 11, 2016 22:02
-
-
Save diegommarino/a773f0a1fc73f59bc59d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package br.unifor.exercise_2_6; | |
//Compresser Centralizado | |
import java.io.File; | |
import java.io.FileInputStream; | |
import java.io.FileNotFoundException; | |
import java.io.FileOutputStream; | |
import java.io.IOException; | |
import java.util.ArrayList; | |
import java.util.Date; | |
import java.util.List; | |
import java.util.zip.ZipEntry; | |
import java.util.zip.ZipOutputStream; | |
public class Compresser { | |
public static void compress(String fileName){ | |
FileOutputStream fos; | |
try { | |
fos = new FileOutputStream("atest.zip"); | |
ZipOutputStream zos = new ZipOutputStream(fos); | |
addToZipFile(fileName, zos); | |
zos.close(); | |
fos.close(); | |
} catch (FileNotFoundException e) { | |
// TODO Auto-generated catch block | |
e.printStackTrace(); | |
} catch (IOException e) { | |
// TODO Auto-generated catch block | |
e.printStackTrace(); | |
} | |
} | |
public static void addToZipFile(String fileName, ZipOutputStream zos) throws FileNotFoundException, IOException { | |
System.out.println("Writing '" + fileName + "' to zip file"); | |
File file = new File(fileName); | |
FileInputStream fis = new FileInputStream(file); | |
ZipEntry zipEntry = new ZipEntry(fileName); | |
zos.putNextEntry(zipEntry); | |
byte[] bytes = new byte[1024]; | |
int length; | |
while ((length = fis.read(bytes)) >= 0) { | |
zos.write(bytes, 0, length); | |
} | |
zos.closeEntry(); | |
fis.close(); | |
} | |
public static void main(String[] args) throws FileNotFoundException, IOException { | |
String basePath = "/home/jaga/"; | |
List<String> fileNames = new ArrayList<String>(); | |
String fileName = ""; | |
for (String a : args) { | |
fileName = basePath + a + ".txt"; | |
long tempoInicio = System.currentTimeMillis(); | |
Compresser.compress(fileName); | |
System.out.println("Tempo Total: "+(System.currentTimeMillis()-tempoInicio)+" milisegundos."); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment