From below two programs helps to do concurrent zipping of all files in folder using java.
import java.io.File; import java.io.FilenameFilter; import java.util.ArrayList; import java.util.Arrays; import java.util.List; import java.util.concurrent.Callable; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.Future; import com.google.common.collect.Lists; public class ZipIt { public static void main(String[] args){ try { File dir=new File("<DIR PATH>"); File[] xmlFiles=null; if (dir.isDirectory()) { xmlFiles = dir.listFiles(new FilenameFilter() { @Override public boolean accept(File folder, String name) { return name.toLowerCase().endsWith(".xml"); } }); } ExecutorService executorService = Executors.newScheduledThreadPool(60); List<List<File>> smallerLists = Lists.partition(Arrays.asList(xmlFiles), 60); List<Future<Integer>> futures=null; //20 independent threads used to generate 20 images. List<Callable<Integer>> callables = new ArrayList<Callable<Integer>>(); for (List<File> list : smallerLists) { callables.add(new ZipFilesThread(list)); } try { futures = executorService.invokeAll(callables); } finally { executorService.shutdown(); } for (Future<Integer> future : futures) { System.out.println("File converted to Zip:"+future.isDone()); } } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
import java.io.File; import java.util.List; import java.util.concurrent.Callable; import java.util.logging.Logger; import com.mercuria.etl.util.ZipUtil; public class ZipFilesThread implements Callable<Integer> { List<File> files; private static Logger logger = Logger.getLogger(ZipFilesThread.class.getName()); public ZipFilesThread(List<File> files) { super(); this.files=files; } @Override public Integer call() throws Exception { for (File xmlFile : files) { ZipUtil.zipFile(xmlFile.getAbsolutePath()); xmlFile.delete(); } return files.size(); } }
No comments:
Post a Comment