Import java.util.zip.ZipEntry; Import java.util.zip.ZipOutputStream; to Import org.apache.tools.zip.ZipEntry; Import org.apache.tools.zip.ZipOutputStream;
Ant package provides classes setEncoding ZipOutputStream ( "gbk") method.
zos.setEncoding ( "gbk");
ant package download (click to enter the page, opened a new page)
Download the following address:
https://mvnrepository.com/artifact/org.apache.ant/ant/1.7.1 (click to enter the page, the page is covered)
package file; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import org.apache.tools.zip.ZipEntry; import org.apache.tools.zip.ZipOutputStream; public class Zip { public static void main(String[] args) { //文件所在路径 String path = "E:\\test"; File dir = new File(path); IF (! dir.isDirectory ()) { System.out.println (path + "-! path does not exist" ); } // All files File [] = Files new new File (path) .listFiles (); IF ( == files null || files.length <. 1 ) { System.out.println (path + "- no file path!" ); } // archive name String zipname = "hello.zip" ; file ZipFile = new new File (path + the File.separator + zipname); IF (ZipFiles (Files, ZipFile) == 0 ) { System.out.println ("Compressed file OK!" ); } } Public static int ZipFiles (File [] Files, File ZipFile) { IF (zipFile.exists ()) { System.out.println (ZipFile + "- archive already exists!" ); return -1 ; } BufferedInputStream BIS = null ; the ZipOutputStream ZOS = null ; the try { ZOS = new new the ZipOutputStream ( new new BufferedOutputStream The ( new newA FileOutputStream (ZipFile))); byte [] = BUFS new new byte [1024 * 10 ]; for ( int I = 0; I <files.length; I ++ ) { // create ZIP entity and add compressed ZipEntry zipEntry = new new the ZipEntry (Files [I] .getName ()); zos.putNextEntry (ZipEntry); // garbled zos.setEncoding ( "GBK" ); BIS = new new BufferedInputStream ( new new the FileInputStream (Files [I]), 1024 * 10 ); int Read = 0; while((read=bis.read(bufs, 0, 1024*10)) != -1){ zos.write(bufs,0,read); } } return 0; }catch (IOException e) { e.printStackTrace(); System.out.println("压缩文件过程BUG了!"); return -1; } finally { try { if (zos != null) { zos.close(); } } catch (Exception e) { e.printStackTrace(); } try { if (bis != null) { bis.close(); } } catch (Exception e) { e.printStackTrace(); } } } }