就这个工具类,使用ZipUtil.pack方法进行打包的时候,如果你将要打包的文件是在同一目录下,不存在分层的情况,那是没有任何问题的,但是如果存在分层且!!!!文件夹名称是全中文的时候,就会存在一些问题,具体表现形式是 某些解压软件可以正常打开 例如winRAR,但是有些软件不能正常打开 例如360解压 如图
源文件
通过工具类打包过后 winRaR 打开
一切正常 非常完美。 但是用360压缩打开。。。
如图 文件夹变成 空白文件了,且解压报错。
具体原因是 该工具类底层源码使用File.list()方法获取源文件夹所有文件的名称,在进行copy(打包)过程时,如果判断当前文件(举例 假设当前文件是 A )是文件夹时会进行创建文件夹动作,这步存在问题,如果文件A下面没有文件,这样写没问题,如果文件A下存在文件,那这一步就是多余的,因为在copy文件A下的文件的时候 会默认创建出父级目录,这样就会产生创建两次的操作,在文件A名称是中文名的情况下,会产生问题,导致部分解压工作不能正确解压。
所以该工具类存在问题,只能自己封装了,好在zip打包并不复杂,最底层是使用ZipOutputStream类,写出来简简单单。
附上自己代码 ,各位可以按照自己业务需求自己魔改一下:
/** * Zip 打包 * @throws IOException */ public static void zipPack(String source,String target)throws IOException { ZipOutputStream out = new ZipOutputStream(new FileOutputStream(target)); File file =new File(source); File[] oldFile =file.listFiles(); FileInputStream inputStream; for (int i = 0; i <oldFile.length ; i++) { if (oldFile[i].listFiles()!=null){ for (File f: oldFile[i].listFiles()) { out.putNextEntry(new ZipEntry(oldFile[i].getName()+"/"+f.getName())); byte[] buffer = new byte[4096]; int readLength; inputStream=new FileInputStream(f); while ((readLength = inputStream.read(buffer)) > 0) { byte[] bytes = new byte[readLength]; System.arraycopy(buffer, 0, bytes, 0, readLength); out.write(bytes); } out.closeEntry(); } }else { out.putNextEntry(new ZipEntry(oldFile[i].getName())); byte[] buffer = new byte[4096]; int readLength; inputStream=new FileInputStream(oldFile[i]); while ((readLength = inputStream.read(buffer)) > 0) { byte[] bytes = new byte[readLength]; System.arraycopy(buffer, 0, bytes, 0, readLength); out.write(bytes); } out.closeEntry(); } } out.close(); }