Many of my friends will use JDK's built-in zip-related APIs to compress and decompress the zip file. This function is no problem if the zip file is used in full English, but it cannot be used in Chinese, next I will introduce a program that supports Chinese Java compression and decompression zip files.
Apache ant:
Http://ant.apache.org/bindownload.cgi
Put lib/ant. jar in the build path of our project. You only need ant. jar. In fact, ant's zip API is highly similar to jdk's. If it was previously written using jdk's api, basically you only need to change the import package at the top.
The Code is as follows: |
Copy code |
Package common; Import java. io. BufferedInputStream; Import java. io. BufferedOutputStream; Import java. io. File; Import java. io. FileInputStream; Import java. io. FileOutputStream; Import java. util. Enumeration; Import java. util. List; Import org.apache.tools.zip. ZipEntry; Import org.apache.tools.zip. ZipFile; Import org.apache.tools.zip. ZipOutputStream; Public class ZipUtils { Public static void main (String [] args) throws Exception { UnzipPSD ("/media/share/material/file/temp/1eeb28ecb8e0d6f2.zip", "/media/share/material/file/temp /"); } /** * Decompress the zip file, * * @ Param zipFile contains multiple psd files and supports multi-level directories. * @ Param targetPath: Save the Directory * @ Throws Exception */ Public static void unzipPSD (String zipPath, String targetPath) throws Exception { ZipFile zipFile = new ZipFile (zipPath ); Enumeration emu = zipFile. getEntries (); Int I = 0; While (emu. hasMoreElements ()){ ZipEntry entry = (ZipEntry) emu. nextElement (); String fileName = entry. getName (). toLowerCase (); If (! FileName. startsWith ("_ macosx/") & amp; fileName. endsWith ("psd ")) { // If the file name does not start with _ macosx/and ends with psd, It is the psd file. decompress the file, and the _ macosx directory is automatically added to the compressed file under mac, but it is useless. BufferedInputStream bis = new BufferedInputStream ( ZipFile. getInputStream (entry )); File file = new File (targetPath + System. currentTimeMillis () + ". psd "); // 40 K read at a time Int BUFFER = 40960; FileOutputStream fos = new FileOutputStream (file ); BufferedOutputStream bos = new BufferedOutputStream (fos, BUFFER ); Int count; Byte data [] = new byte [BUFFER]; While (count = bis. read (data, 0, BUFFER ))! =-1 ){ Bos. write (data, 0, count ); } Bos. flush (); Bos. close (); Bis. close (); } } ZipFile. close (); } /** * Compressing multiple files * @ Param zipPath * @ Param filePaths */ Public static void zipFiles (String zipPath, List filePaths) { Try { BufferedInputStream origin = null; FileOutputStream dest = new FileOutputStream (zipPath ); ZipOutputStream out = new ZipOutputStream (new BufferedOutputStream ( Dest )); Int BUFFER = 40960; Byte data [] = new byte [BUFFER]; For (String filepha: filePaths) { File file = new File (filepa ); FileInputStream fi = new FileInputStream (file ); Origin = new BufferedInputStream (fi, BUFFER ); ZipEntry entry = new ZipEntry (file. getName ()); Out. putNextEntry (entry ); Int count; While (count = origin. read (data, 0, BUFFER ))! =-1 ){ Out. write (data, 0, count ); } Origin. close (); } Out. close (); } Catch (Exception e ){ E. printStackTrace (); } } }
|
Original article: Application Development notes