1. 7zip Compress Example
Download: commons-compress-1.16.1
Download: xz for java 1.8
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | public static void z7zip(File baseDir, List<File> list, File outFile) throws IOException { SevenZOutputFile z7 = new SevenZOutputFile(outFile); if (list.size() > 0) { for (int i = 0; i < list.size(); i++) { String entryName = list.get(i).getPath().substring(baseDir.getPath().length()+1); z7.putArchiveEntry(z7.createArchiveEntry( list.get(i), entryName)); FileInputStream fis = new FileInputStream(list.get(i)); byte[] buf = new byte[8 * 1024]; int length; while ((length = fis.read(buf, 0, buf.length)) >= 0) { z7.write(buf, 0, length); } fis.close(); z7.closeArchiveEntry(); } } z7.close(); } static void z7zipTest() throws IOException { Compress cu = new Compress(); List<File> filelist = new ArrayList<File>(); filelist.add(new File("c:/temp/a.jar")); filelist.add(new File("c:/temp/b/c.jar")); File dir = new File("c:/temp"); File zippedFile = new File ( "c:/temp/test.7z"); cu.z7zip(dir, filelist, zippedFile); } |
Result:
2. 7zip Decompress Example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | public static void z7unzip( File inFile, File destDir ) throws IOException { SevenZFile z7 = new SevenZFile(inFile); SevenZArchiveEntry entry; destDir.mkdirs(); while ( (entry = z7.getNextEntry()) != null ){ String name = entry.getName(); File target = new File (destDir, name); if ( entry.isDirectory() ){ target.mkdirs(); } else { if( ! target.getParentFile().exists() ) { target.getParentFile().mkdirs(); } target.createNewFile(); BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(target)); byte [] buf = new byte[1024 * 8]; int len = 0; while ((len = z7.read(buf)) >= 0 ){ bos.write(buf, 0, len); } bos.close(); } } z7.close(); } static void z7unzipTest() throws IOException { Compress cu = new Compress(); File dir = new File("c:/temp1"); File zippedFile = new File ( "c:/temp/test.7z"); cu.z7unzip( zippedFile, dir); } |
Result:
댓글 없음:
댓글 쓰기