1. Zip 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 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 | import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import net.lingala.zip4j.core.ZipFile; import net.lingala.zip4j.exception.ZipException; import net.lingala.zip4j.model.ZipParameters; import net.lingala.zip4j.util.Zip4jConstants; public class ZipUtil { ZipFile zipFile = null; ZipParameters parameters; public ZipUtil(String zipFileName) throws IOException, ZipException { zipFile = new ZipFile(zipFileName); parameters = new ZipParameters(); parameters.setCompressionMethod(Zip4jConstants.COMP_DEFLATE); parameters.setCompressionLevel(Zip4jConstants.DEFLATE_LEVEL_ULTRA); } public void addFolder(String folder, boolean includeRootFolder) throws ZipException { parameters.setSourceExternalStream(false); parameters.setFileNameInZip(null); parameters.setIncludeRootFolder(includeRootFolder); zipFile.addFolder(folder, parameters); } public void addFile(String file, String target) throws FileNotFoundException, ZipException { parameters.setSourceExternalStream(true); parameters.setFileNameInZip(target); zipFile.addStream(new FileInputStream(file), parameters); } public void setRootFolderInZip(String folder) { parameters.setRootFolderInZip(folder); } public void setIncludeRootFolder(boolean bool) { parameters.setIncludeRootFolder(bool); } public void setFileNameInZip(String fileName) { parameters.setFileNameInZip(fileName); } /* public static void main(String[] args) throws IOException, ZipException { ZipUtil zip = new ZipUtil("D:/zip4j4.zip"); zip.addFile("D:/Temp2/a.png", "test5/xxx.png"); zip.setIncludeRootFolder(false); zip.setRootFolderInZip("test2"); zip.addFolder("D:/Temp1/b/e/FxLibs", true); zip.setRootFolderInZip("test2"); } */ } |
2. Unzip 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 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 | import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; import net.lingala.zip4j.core.ZipFile; import net.lingala.zip4j.exception.ZipException; import net.lingala.zip4j.model.FileHeader; public class UnzipUtil { ZipFile zipFile = null; public UnzipUtil() { } public UnzipUtil(String zipFileName) throws IOException, ZipException { zipFile = new ZipFile(zipFileName); } public void load(String zipFileName) throws ZipException { zipFile = new ZipFile(zipFileName); } public void extractAll(String target) throws ZipException { zipFile.extractAll(target); } public void extractFile(int index, String target) throws ZipException { zipFile.extractFile((FileHeader) zipFile.getFileHeaders().get(index), target); } public void extractFile(String name, String target) throws ZipException { zipFile.extractFile(zipFile.getFileHeader(name), target); } public int size() { try { return zipFile.getFileHeaders().size(); } catch (ZipException e) { e.printStackTrace(); return 0; } } public String name(int index) throws ZipException { return ((FileHeader) zipFile.getFileHeaders().get(index)).getFileName(); } public byte[] unzipEntry(FileHeader zipEntry) throws IOException, ZipException { if( zipEntry == null ) { return null; } if( zipEntry.isDirectory() ) { return null; } return copyStreamToByteArray( zipFile.getInputStream(zipEntry) ); } public byte[] unzipEntry(String name) throws IOException, ZipException { return unzipEntry( zipFile.getFileHeader(name)); } public byte[] unzipEntry(int index) throws IOException, ZipException { return unzipEntry( (FileHeader) zipFile.getFileHeaders().get(index)); } public byte[] copyStreamToByteArray(InputStream input ) throws IOException { ByteArrayOutputStream output = new ByteArrayOutputStream(); byte[] buffer = new byte[1024]; int n = 0; while( (n = input.read(buffer)) > 0 ) { output.write(buffer, 0, n); } output.close(); return output.toByteArray(); } /* public static void main(String[] args) throws IOException, ZipException { UnzipUtil unzip = new UnzipUtil("D:/a.zip"); unzip.extractAll("D:/Temp9"); UnzipUtil uz = new UnzipUtil("D:/a.zip"); for( int i = 0; i < uz.size(); i++ ) { System.out.println(uz.name(i)); byte[] fileData = uz.unzipEntry(i); System.out.println("----------------------------------------"); System.out.println(uz.name(i)); System.out.println("----------------------------------------"); System.out.println(new String(fileData)); } } */ } |
댓글 없음:
댓글 쓰기