
一、文件紧缩的中文乱码问题
1、中文文件名的乱码处置惩罚
关于紧缩的文件,当文件称号是中文时,若运用JDK API中自带的类(java.util.zip.ZipEntry; java.util.zip.ZipOutputStream;)举行紧缩,紧缩完成后,能够看到紧缩包中的文件称号是乱码(文件的内容无乱码问题)。
此时只要用ANT中的ant.jar中的类(org.apache.tools.zip.ZipEntry; org.apache.tools.zip.ZipOutputStream;)就能够处置惩罚此问题。
引荐:java视频教程
2、ant.jar依靠
<!-- ant.jar 用于处置惩罚文件解紧缩乱码问题 --> <dependency> <groupId>ant</groupId> <artifactId>ant</artifactId> <version>1.6.5</version> </dependency>
二、JAVA完成ZIP紧缩源代码
import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStream; import org.apache.tools.zip.ZipEntry; import org.apache.tools.zip.ZipOutputStream; import org.slf4j.Logger; import org.slf4j.LoggerFactory; /** * zip紧缩工具包 * @Class ZipUtils */ public class ZipUtils { private final static Logger logger = LoggerFactory.getLogger(ZipUtils.class); private static final int BUFFER_SIZE = 2 * 1024; /** * 紧缩成ZIP * @param srcFilePath 紧缩文件途径 * @param tarFilePath 目的ZIP输出途径 * @param KeepDirStructure 是不是保存本来的目次组织,true:保存目次组织; * false:一切文件跑到紧缩包根目次下(注重:不保存目次组织可能会涌现同名文件,会紧缩失利) * @throws Exception 紧缩失利会抛出非常 */ public static boolean toZip(String srcFilePath, String tarFilePath, boolean KeepDirStructure) throws Exception { boolean isCompressSuccess = false; long start = System.currentTimeMillis(); FileOutputStream fos = null; ZipOutputStream zos = null; try { File sourceFile = new File(srcFilePath); if (!sourceFile.exists()) { throw new FileNotFoundException("待紧缩文件 [" + srcFilePath + "]不存在."); } fos = new FileOutputStream(new File(tarFilePath)); zos = new ZipOutputStream(fos); // 设置紧缩的编码,处置惩罚紧缩途径中的中文乱码问题 zos.setEncoding("UTF-8"); compress(sourceFile, zos, sourceFile.getName(), KeepDirStructure); isCompressSuccess = true; long end = System.currentTimeMillis(); logger.info("【文件紧缩】 紧缩完成,耗时:{} ms", (end - start)); } catch (Exception e) { logger.error("【文件紧缩】 紧缩失利", e); throw new RuntimeException("文件紧缩失利", e); } finally { closeOutPutStream(zos); closeOutPutStream(fos); } return isCompressSuccess; } /** * 递归紧缩要领 * @param sourceFile 源文件 * @param zos zip输出流 * @param name 紧缩后的称号 * @param KeepDirStructure 是不是保存本来的目次组织,true:保存目次组织; * false:一切文件跑到紧缩包根目次下(注重:不保存目次组织可能会涌现同名文件,会紧缩失利) * @throws Exception */ private static void compress(File sourceFile, ZipOutputStream zos, String name, boolean KeepDirStructure) throws Exception { byte[] buf = new byte[BUFFER_SIZE]; if (sourceFile.isFile()) { // 向zip输出流中增加一个zip实体,组织器中name为zip实体的文件的名字 zos.putNextEntry(new ZipEntry(name)); // copy文件到zip输出流中 int len; FileInputStream in = new FileInputStream(sourceFile); while ((len = in.read(buf)) != -1) { zos.write(buf, 0, len); } zos.closeEntry(); in.close(); } else { File[] listFiles = sourceFile.listFiles(); if (listFiles == null || listFiles.length == 0) { // 须要保存本来的文件组织时,须要对空文件夹举行处置惩罚 if (KeepDirStructure) { // 空文件夹的处置惩罚 zos.putNextEntry(new ZipEntry(name + "/")); // 没有文件,不须要文件的copy zos.closeEntry(); } } else { for (File file : listFiles) { // 推断是不是须要保存本来的文件组织 if (KeepDirStructure) { // 注重:file.getName()前面须要带上父文件夹的名字加一斜杠, // 不然末了紧缩包中就不能保存本来的文件组织,即:一切文件都跑到紧缩包根目次下了 compress(file, zos, name + "/" + file.getName(), KeepDirStructure); } else { compress(file, zos, file.getName(), KeepDirStructure); } } } } } /** * 开释资本 * @Title closeOutPutStream * @param ops * @return void */ public static void closeOutPutStream(OutputStream ops) { if (ops != null) { try { ops.close(); } catch(IOException ex) { logger.error("封闭输出流失利", ex); } } } }
更多java学问请关注java基础教程栏目。
以上就是java文件紧缩乱码处置惩罚要领的细致内容,更多请关注ki4网别的相干文章!