Untitled
unknown
java
2 years ago
2.0 kB
1
Indexable
Never
import java.io.*; import java.util.zip.GZIPOutputStream; import java.util.concurrent.Executors; import java.util.concurrent.ExecutorService; public class Main { public static void zippingFile(File f , String filePath) throws IOException { String zippedPath = filePath + ".gz"; FileInputStream fis = new FileInputStream(filePath); FileOutputStream fos = new FileOutputStream(zippedPath); GZIPOutputStream gzipOS = new GZIPOutputStream(fos); byte[] buffer = new byte[(int) f.length()]; int len; while ((len = fis.read(buffer)) != -1) { gzipOS.write(buffer, 0, len); } System.out.printf("%s %s zipped%n", Thread.currentThread().getName(), f.getName()); gzipOS.close(); fos.close(); fis.close(); f.delete(); } public static void zipping(String fileName) throws IOException { File f = new File(fileName); if (f.isDirectory()) { for (File file : f.listFiles()) { if (file.isFile()) { zippingFile(file, file.getAbsolutePath()); } else { zipping(file.getAbsolutePath()); } } } else { zippingFile(f, f.getAbsolutePath()); } } public static class Task implements Runnable { String fileName; public Task(String fileName) { this.fileName = fileName; } @Override public void run() { try { zipping(fileName); } catch (IOException e) { throw new RuntimeException(e); } } } public static void main(String[] args) { ExecutorService service = Executors.newFixedThreadPool(5); for (int i = 0; i < args.length; i++) { service.execute(new Task(args[i])); } System.out.println(Thread.currentThread().getName()); service.shutdown(); } }