JAVA解压ZIP格式的压缩包
Java代码实现rar解压最全攻略操作

Java代码实现rar解压最全攻略操作⼀、通过[com.github.junrar]实现winrar5.0以下版本解压1、⾸先贴出来maven依赖,这⾥使⽤的是最⾼版本4.0,但是依然⽆法解决5.0及其以上的版本问题。
<!-- https:///artifact/com.github.junrar/junrar --><dependency><groupId>com.github.junrar</groupId><artifactId>junrar</artifactId><version>4.0.0</version></dependency>2、代码实现:优化了https:///fakergoing/article/details/82260699中的linux中\⽆法识别问题/*** 根据原始rar路径,解压到指定⽂件夹下* 这种⽅法只能解压rar 5.0版本以下的,5.0及其以上的⽆法解决** @param srcRarPath 原始rar路径+name* @param dstDirectoryPath 解压到的⽂件夹*/public static String unRarFile(String srcRarPath, String dstDirectoryPath) throws Exception {log.debug("unRarFile srcRarPath:{}, dstDirectoryPath:{}", srcRarPath, dstDirectoryPath);if (!srcRarPath.toLowerCase().endsWith(".rar")) {log.warn("srcFilePath is not rar file");return "";}File dstDiretory = new File(dstDirectoryPath);// ⽬标⽬录不存在时,创建该⽂件夹if (!dstDiretory.exists()) {dstDiretory.mkdirs();}// @Cleanup Archive archive = new Archive(new File(srcRarPath)); com.github.junrar 0.7版本jarAPI@Cleanup Archive archive = new Archive(new FileInputStream(new File(srcRarPath)));if (archive != null) {// 打印⽂件信息archive.getMainHeader().print();FileHeader fileHeader = archive.nextFileHeader();while (fileHeader != null) {// 解决中⽂乱码问题【压缩⽂件中⽂乱码】String fileName = fileHeader.getFileNameW().isEmpty() ? fileHeader.getFileNameString() : fileHeader.getFileNameW();// ⽂件夹if (fileHeader.isDirectory()) {File fol = new File(dstDirectoryPath + File.separator + fileName.trim());fol.mkdirs();} else { // ⽂件// 解决linux系统中\分隔符⽆法识别问题String[] fileParts = fileName.split("\\\\");StringBuilder filePath = new StringBuilder();for (String filePart : fileParts) {filePath.append(filePart).append(File.separator);}fileName = filePath.substring(0, filePath.length() - 1);File out = new File(dstDirectoryPath + File.separator + fileName.trim());if (!out.exists()) {// 相对路径可能多级,可能需要创建⽗⽬录.if (!out.getParentFile().exists()) {out.getParentFile().mkdirs();}out.createNewFile();}@Cleanup FileOutputStream os = new FileOutputStream(out);archive.extractFile(fileHeader, os);}fileHeader = archive.nextFileHeader();}} else {log.warn("rar file decompression failed , archive is null");}return dstDirectoryPath;}3、该⽅法弊端最⼤的问题就在于⽆法实现winrar5.0及其以上版本的解压问题:WinRAR5之后,在rar格式的基础上,推出了另⼀种rar,叫RAR5,winrar 官⽅并没有开源算法,jar包⽆法解析这种格式。
Java解压ZIP文件的简单操作

}
entries.add(entry);
}
unzip(zipFile, entries, outputDirectory);
}
public static void unzip(ZipFile zipFile, List<ZipEntry> entries,
MultiThreadEntry mte = new MultiThreadEntry(zipFile, zipEntry,
outputDirectory);
Thread thread = new Thread(mte);
thread.start();
}
}
e1.printStackTrace();
}
} finally {
try {
bis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public void unzipFiles(ZipEntry zipEntry, String outputDirectory)
/*
* Use MultiThread to read each entry
*/
private static class MultiThreadEntry implements Runnable {
public static final int BUFFER_SIZE = 2048;
*/
public ZipUtils() {
}
public static void unZipDirectory(String zipFileDirectory,
zip文件怎么解压

zip文件怎么解压标题:zip文件怎么解压摘要:zip文件是一种常见的文件压缩格式,它可以将多个文件或者文件夹打包成一个单一的文件,从而减小文件的大小并方便传输。
本文将向您介绍如何解压zip文件,以及使用不同操作系统和软件工具提供的解压缩方法。
一、使用Windows系统自带解压工具1. 右键单击要解压的zip文件。
2. 在右键菜单中选择“提取所有文件”选项。
3. 在弹出的提取窗口中选择解压缩文件的目标位置。
4. 点击“提取”按钮,等待解压过程完成。
二、使用Mac系统自带解压工具1. 双击要解压的zip文件。
2. Mac系统会自动解压缩文件并将解压后的文件显示在与原文件相同的目录中。
三、使用第三方解压工具除了操作系统自带的解压工具外,还有许多第三方工具可以帮助您解压zip文件,并提供更丰富的功能和选项。
以下是几个常用的跨平台解压软件推荐:1. WinRAR:一款功能强大且广泛使用的文件压缩和解压缩软件。
在Windows系统上使用WinRAR,您只需右键单击zip文件并选择“解压缩到当前文件夹”即可自动解压文件。
2. 7-Zip:一款开源的压缩和解压缩软件,支持多种格式的压缩文件。
在Windows系统上使用7-Zip,您只需右键单击zip文件并选择“7-Zip”>“提取到当前文件夹”即可进行解压。
3. WinZip:一款用户友好的文件压缩和解压缩软件,提供了许多高级压缩和解压缩功能。
在Windows系统上使用WinZip,您只需双击zip文件,然后点击“解压缩”按钮即可进行解压。
四、解压缩其他压缩格式的文件除了zip文件,还有许多其他常见的压缩格式,如rar、tar、gzip 等。
对于这些格式的文件解压缩,可以使用相应的解压工具,具体步骤和操作方式类似,只需选择相应的解压选项即可。
五、注意事项和常见问题1. 确保您下载的zip文件完整无损,否则解压可能会出现错误或文件损坏。
2. 在解压zip文件之前,最好先查看压缩文件中的内容,以确保不会解压到非期望的位置或文件夹。
JavaZIP压缩和解压缩文件(解决中文文件名乱码问题)

JavaZIP压缩和解压缩⽂件(解决中⽂⽂件名乱码问题)JDK中⾃带的ZipOutputStream在压缩⽂件时,如果⽂件名中有中⽂,则压缩后的zip⽂件打开时发现中⽂⽂件名变成乱码.解决的⽅法是使⽤apache-ant-zip.jar包(见附件)中的ZipOutputStream和ZipEntry.即,导⼊类:import org.apache.tools.zip.ZipEntry;import org.apache.tools.zip.ZipOutputStream;并且注意,压缩之前调⽤ZipOutputStream的out.setEncoding(System.getProperty("sun.jnu.encoding"));⽅法,系统参数sun.jnu.encoding表⽰获取当前系统中的⽂件名的编码⽅式.这⾥将ZipOutputStream的⽂件名编码⽅式设置成系统的⽂件名编码⽅式.解压时,直接使⽤JDK原来的ZipInputStream即可.但是有个需要注意的地⽅是,在读取ZIP⽂件之前,需要设置:System.setProperty("sun.zip.encoding", System.getProperty("sun.jnu.encoding"));将系统的ZIP编码格式设置为系统⽂件名编码⽅式,否则解压时报异常.import java.util.zip.ZipEntry;import java.util.zip.ZipOutputStream;改为import org.apache.tools.zip.ZipEntry;import org.apache.tools.zip.ZipOutputStream;ant包⾥提供ZipOutputStream类的setEncoding("gbk")⽅法。
zos.setEncoding("gbk");。
JAVAzip解压MALFORMED错误

JAVAzip解压MALFORMED错误最近在在使⽤zip 解压时,使⽤JDK1.7及以上版本在解压时,某些⽂件会报异常Exception in thread "main" ng.IllegalArgumentException: MALFORMEDat java.util.zip.ZipCoder.toString(ZipCoder.java:58)at java.util.zip.ZipFile.getZipEntry(ZipFile.java:567)at java.util.zip.ZipFile.access$900(ZipFile.java:61)at java.util.zip.ZipFile$ZipEntryIterator.next(ZipFile.java:525)at java.util.zip.ZipFile$ZipEntryIterator.nextElement(ZipFile.java:500)at java.util.zip.ZipFile$ZipEntryIterator.nextElement(ZipFile.java:481)at zip.ReadingArchive.unzip(ReadingArchive.java:36)at zip.ReadingArchive.unzip(ReadingArchive.java:82)at zip.ReadingArchive.unzip(ReadingArchive.java:82)at main.Main.main(Main.java:13)究其原因是因为⽂件名中⽂所致,⽽ZIPFile默认使⽤UTF-8,在遇到解压⾮UTF-8的时候就会报错public ZipFile(File file, int mode) throws IOException {this(file, mode, StandardCharsets.UTF_8);}最终我⽤的解决⽅法为Charset gbk = Charset.forName("GBK");ZipFile zipFile = new ZipFile(zipFileName, gbk);当然这个肯定不适⽤与所有环境,可以使⽤的修改字符集来解决,仅供参考。
java解压zip乱码问题文库

java解压zip乱码问题(2009-08-27 18:59:01)转载▼标签:杂谈在网上找了很多的资料,大概上有两种解决办法:第一种是用java.util.zip,可是这种java给写死了编码格式指定为了UTF-8的,所以乱码问题是没办法处理的,看有些人说可以改src.zip里面的类ZipInputStream和ZipOutputStream来解决乱码问题,具体能不能解决就不知道了,本人也没有去测试(因为我不喜欢这种处理方式,感觉这是治标不治本的做法)。
第二种方法是用ant.jar带的org.apache.tools.zip,而且看了很多类似的文章说能够解决乱码问题了,也许是自己没弄明白吧,把代码拷来测试的时候是能够正常的解压缩了,可乱码问题还是有的,我开发环境编码是UTF-8的,一样的代码不知道他们乱码问题是怎么处理的,我还得多学习学习,花了很长的时间研究代码,后来改动了下结果乱码问题就解决了,可能是和我的编码格式有关吧,把我整理的代码放这方便以后可以在看看(都是在网上找的资料,呵呵不是本人写的)package com.bjtxx.lms.server.tools;import java.io.File;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.IOException;import java.io.InputStream;import org.apache.tools.zip.ZipOutputStream;public class ZipTest {public static void zip(ZipOutputStream out, File f, String base, boolean first) throws Exception {if (first) {if (f.isDirectory()) {out.putNextEntry(new org.apache.tools.zip.ZipEntry("/"));base = base + f.getName();first = false;} elsebase = f.getName();}if (f.isDirectory()) {File[] fl = f.listFiles();base = base + "/";for (int i = 0; i < fl.length; i++) {zip(out, fl[i], base + fl[i].getName(), first);}} else {out.putNextEntry(new org.apache.tools.zip.ZipEntry(base));FileInputStream in = new FileInputStream(f);int b;System.out.println(base);while ((b = in.read()) != -1) {out.write(b);}in.close();}}@SuppressWarnings("unchecked")public static void unZipFileByOpache(org.apache.tools.zip.ZipFile zipFile,String unZipRoot) throws Exception, IOException {java.util.Enumeration e = zipFile.getEntries();System.out.println(zipFile.getEncoding());org.apache.tools.zip.ZipEntry zipEntry;while (e.hasMoreElements()) {zipEntry = (org.apache.tools.zip.ZipEntry) e.nextElement();InputStream fis = zipFile.getInputStream(zipEntry);if (zipEntry.isDirectory()) {} else {File file = new File(unZipRoot + File.separator + zipEntry.getName()); File parentFile = file.getParentFile();parentFile.mkdirs();FileOutputStream fos = new FileOutputStream(file);byte[] b = new byte[1024];int len;while ((len = fis.read(b, 0, b.length)) != -1) {fos.write(b, 0, len);}fos.close();fis.close();}}}public static void ZipFile(String zipFileName, String inputFileName)throws Exception {org.apache.tools.zip.ZipOutputStream out = new org.apache.tools.zip.ZipOutputStream(new FileOutputStream(zipFileName));out.setEncoding("gbk");//设置的和文件名字格式一样或开发环境编码设置一样的话就能正常显示了File inputFile = new File(inputFileName);zip(out, inputFile, "", true);System.out.println("zip done");out.close();}public static void unZipFile(String unZipFileName, String unZipPath)throws Exception {org.apache.tools.zip.ZipFile zipFile = new org.apache.tools.zip.ZipFile(unZipFileName, "gbk");unZipFileByOpache(zipFile, unZipPath);System.out.println("unZip Ok");}public static void main(String[] args) throws Exception {ZipFile("e:zf.zip", "e:zf.xml");// unZipFile("e:zf.zip", "e:");}}。
java解压zipjava.lang.IllegalArgumentException:M。。。
然后跟提供了两种办法
1.更改JDK的源码。 2.使用ant中的zip类。可以设定字符集。
然后就换成了ant.jar包 1. ng.IllegalArgumentException: MALFORMED 2. at java.util.zip.ZipCoder.toString(Unknown Source) 3. at java.util.zip.ZipInputStream.readLOC(Unknown Source) 4. at java.util.zip.ZipInputStream.getNextEntry(Unknown Source) 5. at com.yc.utils.TestZip.extZipFileList(TestZip.java:40) 6. at com.yc.utils.TestZip.main(TestZip.java:21)
博客园 用户登录 代码改变世界 密码登录 短信登录 忘记登录用户名 忘记密码 记住我 登录 第三方登录/注册 没有账户, 立即注册
java解压 ng.IllegalArgumentException: M。。。
ng.IllegalArgumentException: MALFORMED at java.util.zip.ZipCoder.toString(Unknown Source) at java.util.zip.ZipInputStream.readLOC(Unknown Source) at java.util.zip.ZipInputStream.getNextEntry(Unknown Source) at com.yc.utils.TestZip.extZipFileList(TestZip.java:40) at com.yc.utils.TestZip.main(TestZip.java:21)
Java代码中的(解压7z加密版)
Java代码中的(解压7z加密版)maven:需要加上这个下载这两个包<dependency><groupId>net.sf.sevenzipjbinding</groupId><artifactId>sevenzipjbinding</artifactId><version>9.20-2.00beta</version></dependency><dependency><groupId>net.sf.sevenzipjbinding</groupId><artifactId>sevenzipjbinding-all-platforms</artifactId><version>9.20-2.00beta</version></dependency>普通的架构:需要⾃⼰下载sevenzipjbinding-9.20-2.00beta.jarsevenzipjbinding-all-platforms-9.20-2.00beta.jar/**** @Description (解压7z)* @param file7zPath(7z⽂件路径)* @param outPutPath(解压路径)* @param passWord(⽂件密码.没有可随便写,或空)* @return* @throws Exception*/public static int un7z(String file7zPath, final String outPutPath, String passWord) throws Exception { IInArchive archive;RandomAccessFile randomAccessFile;randomAccessFile = new RandomAccessFile(file7zPath, "r");archive = SevenZip.openInArchive(null, new RandomAccessFileInStream(randomAccessFile), passWord); int numberOfItems = archive.getNumberOfItems();ISimpleInArchive simpleInArchive = archive.getSimpleInterface();for (final ISimpleInArchiveItem item : simpleInArchive.getArchiveItems()) {final int[] hash = new int[] { 0 };if (!item.isFolder()) {ExtractOperationResult result;final long[] sizeArray = new long[1];result = item.extractSlow(new ISequentialOutStream() {public int write(byte[] data) throws SevenZipException {try {IOUtils.write(data, new FileOutputStream(new File(outPutPath + File.separator + item.getPath()),true));} catch (Exception e) {e.printStackTrace();}hash[0] ^= Arrays.hashCode(data); // Consume datasizeArray[0] += data.length;return data.length; // Return amount of consumed}},passWord);if (result == ExtractOperationResult.OK) {logger.error("解压成功...." +String.format("%9X | %10s | %s", hash[0], sizeArray[0], item.getPath()));// LogUtil.getLog().debug(String.format("%9X | %10s | %s", hash[0], sizeArray[0], item.getPath()));} else {logger.error("解压失败:密码错误或者其他错误...." +result);// LogUtil.getLog().debug("Error extracting item: " + result);}}}archive.close();randomAccessFile.close();return numberOfItems;}/***不含加密,普通解压**/// 解压.Z⽂件如:D:/test/test.Z D:/test/test.txtpublic static void unZFile(String inFileName, String outFileName) { InputStream inputStream = null;OutputStream outputStream = null;try {inputStream = new FileInputStream(inFileName);inputStream = new UncompressInputStream(inputStream);File file = new File(outFileName);outputStream = new FileOutputStream(file);int bytesRead = 0;byte[] buffer = new byte[100000];while ((bytesRead = inputStream.read(buffer, 0, 100000)) != -1) { outputStream.write(buffer, 0, bytesRead);}} catch (Exception e) {e.printStackTrace();logger.error("unZFile Exception " + e.getMessage());} finally {if(outputStream != null){try {outputStream.close();} catch (IOException e) {e.printStackTrace();logger.error("outputStream Close Exception " + e.getMessage()); }}if(inputStream != null){try {inputStream.close();} catch (IOException e) {e.printStackTrace();logger.error("inputStream Close Exception "+ e.getMessage());}}}}。
Java生成压缩文件(zip、rar格式)
Java⽣成压缩⽂件(zip、rar格式)jar坐标:<dependency><groupId>org.apache.ant</groupId><artifactId>ant</artifactId><version>1.10.5</version></dependency>话不多说,直接上代码package com.demo.student.util;import org.apache.tools.zip.ZipEntry;import org.apache.tools.zip.ZipOutputStream;import java.io.*;/*** ⽣成压缩⽂件(zip,rar 格式)*/public class CompressUtil {/*** @param path 要压缩的⽂件路径* @param format ⽣成的格式(zip、rar)d*/public static void generateFile(String path, String format) throws Exception {File file = new File(path);// 压缩⽂件的路径不存在if (!file.exists()) {throw new Exception("路径 " + path + " 不存在⽂件,⽆法进⾏压缩...");}// ⽤于存放压缩⽂件的⽂件夹String generateFile = file.getParent() + File.separator +"CompressFile";File compress = new File(generateFile);// 如果⽂件夹不存在,进⾏创建if( !compress.exists() ){compress.mkdirs();}// ⽬的压缩⽂件String generateFileName = compress.getAbsolutePath() + File.separator + "AAA" + file.getName() + "." + format;// 输⼊流表⽰从⼀个源读取数据// 输出流表⽰向⼀个⽬标写⼊数据// 输出流FileOutputStream outputStream = new FileOutputStream(generateFileName);// 压缩输出流ZipOutputStream zipOutputStream = new ZipOutputStream(new BufferedOutputStream(outputStream));generateFile(zipOutputStream,file,"");System.out.println("源⽂件位置:" + file.getAbsolutePath() + ",⽬的压缩⽂件⽣成位置:" + generateFileName);// 关闭输出流zipOutputStream.close();}/*** @param out 输出流* @param file ⽬标⽂件* @param dir ⽂件夹* @throws Exception*/private static void generateFile(ZipOutputStream out, File file, String dir) throws Exception {// 当前的是⽂件夹,则进⾏⼀步处理if (file.isDirectory()) {//得到⽂件列表信息File[] files = file.listFiles();//将⽂件夹添加到下⼀级打包⽬录out.putNextEntry(new ZipEntry(dir + "/"));dir = dir.length() == 0 ? "" : dir + "/";//循环将⽂件夹中的⽂件打包for (int i = 0; i < files.length; i++) {generateFile(out, files[i], dir + files[i].getName());}} else { // 当前是⽂件// 输⼊流FileInputStream inputStream = new FileInputStream(file);// 标记要打包的条⽬out.putNextEntry(new ZipEntry(dir));// 进⾏写操作int len = 0;byte[] bytes = new byte[1024];while ((len = inputStream.read(bytes)) > 0) {out.write(bytes, 0, len);}// 关闭输⼊流inputStream.close();}} // 测试public static void main(String[] args) {String path = "";String format = "rar";try {generateFile(path, format);} catch (Exception e) {e.printStackTrace();System.out.println(e.getMessage());}}}结果图:压缩整个⽂件/*** 递归压缩⽂件* @param output ZipOutputStream 对象流* @param file 压缩的⽬标⽂件流* @param childPath 条⽬⽬录*/private static void zip(ZipOutputStream output,File file,String childPath){ FileInputStream input = null;try {// ⽂件为⽬录if (file.isDirectory()) {// 得到当前⽬录⾥⾯的⽂件列表File list[] = file.listFiles();childPath = childPath + (childPath.length() == 0 ? "" : "/")+ file.getName();// 循环递归压缩每个⽂件for (File f : list) {zip(output, f, childPath);}} else {// 压缩⽂件childPath = (childPath.length() == 0 ? "" : childPath + "/")+ file.getName();output.putNextEntry(new ZipEntry(childPath));input = new FileInputStream(file);int readLen = 0;byte[] buffer = new byte[1024 * 8];while ((readLen = input.read(buffer, 0, 1024 * 8)) != -1) {output.write(buffer, 0, readLen);}}} catch (Exception ex) {ex.printStackTrace();} finally {// 关闭流if (input != null) {try {input.close();} catch (IOException ex) {ex.printStackTrace();}}}}/*** 压缩⽂件(⽂件夹)* @param path ⽬标⽂件流* @param format zip 格式 | rar 格式* @throws Exception*/public static String zipFile(File path,String format) throws Exception {String generatePath = "";if( path.isDirectory() ){generatePath = path.getParent().endsWith("/") == false ? path.getParent() + File.separator + path.getName() + "." + format: path.getParent() + path.getName() + "." + format; }else {generatePath = path.getParent().endsWith("/") == false ? path.getParent() + File.separator : path.getParent();generatePath += path.getName().substring(0,path.getName().lastIndexOf(".")) + "." + format;}// 输出流FileOutputStream outputStream = new FileOutputStream( generatePath );// 压缩输出流ZipOutputStream out = new ZipOutputStream(new BufferedOutputStream(outputStream));zip(out, path,"");out.flush();out.close();return generatePath;}使⽤// 使⽤例⼦public static void main(String[] args) {String path = "F:/test";String format = "zip";try {System.out.println(zipFile(new File(path),format));} catch (Exception e) {e.printStackTrace();System.out.println(e.getMessage());}}解压/**** @param sourceZip 待解压⽂件路径* @param destDir 解压到的路径*/public static String unZip(String sourceZip, String destDir) {//保证⽂件夹路径最后是"/"或者"\"if( !destDir.endsWith("/") ){destDir += File.separator;}String newDir = "";File sourceFile = new File(sourceZip);newDir = sourceFile.getName().substring(0,sourceFile.getName().lastIndexOf("."));File destDirFile = new File(destDir + newDir);Project p = new Project();Expand e = new Expand();e.setProject(p);e.setSrc(sourceFile);e.setOverwrite(true);e.setDest(destDirFile);/*ant下的zip⼯具默认压缩编码为UTF-8编码,⽽winRAR软件压缩是⽤的windows默认的GBK或者GB2312编码所以解压缩时要制定编码格式*/e.setEncoding("gbk");e.execute();return destDirFile.getAbsolutePath();}。
Java中ZIP压缩与解压--中文文件名乱码解决办法
Java中ZIP压缩与解压--中⽂⽂件名乱码解决办法Apache Ant有个包专门处理ZIP⽂件,可以指定⽂件名的编码⽅式。
由此可以解决问题。
例如:⽤org.apache.tools.zip.ZipOutputStream代替java.util.zip.ZipOutputStream。
java对於⽂字的编码是以 unicode为基础,因此,若是以ZipInputStream及ZipOutputStream来处理压缩及解压缩的⼯作,碰到中⽂档名或路径,那当然是以unicode来处理。
但是,现在市⾯上的压缩及解压缩软体,例如winzip,却是不⽀援unicode的,⼀碰到档名以unicode编码的档案,它就不处理。
那要如何才能做出让WinRar能够处理的压缩档呢?那就得从修改ZipInputStream及ZipOutputStream对於档名的编码⽅式来着⼿了。
我们可以从jdk的src.zip取得ZipInputStream及ZipOutputStream的原始码来加以修改。
⼀、ZipOutputStream.java1.从jdk的src.zip取得ZipOutputStream.java原始码,另存到⼀个新⽂件中,档名改为CnZipOutputStream.java。
2.开始修改原始码,将class名称改为CnZipOutputStream3.建构式也必须更改为CnZipOutputStream4.新增member,这个member记录编码⽅式private String encoding="UTF-8";5.再新增⼀个建构式(这个建构式可以让这个class在new的时候,设定档名的编码)public CZipOutputStream(OutputStream out,String encoding) {this(out);this.encoding=encoding;}6.找到byte[] nameBytes = getUTF8Bytes();(有⼆个地⽅),将它修改如下:byte[] nameBytes = null;try{if (this.encoding.toUpperCase().equals("UTF-8"))nameBytes =getUTF8Bytes();elsenameBytes= .getBytes(this.encoding);}catch(Exception byteE){nameBytes=getUTF8Bytes();}⼆、ZipInputStream.java1.从jdk的src.zip取得ZipInputStream.java原始码,另存到⼀个新⽂件中,档名改为CnZipInputStream.java。
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
1、需要的引入的jar包
<dependency>
<groupId>net.lingala.zip4j</groupId>
<artifactId>zip4j</artifactId>
<version>1.3.2</version>
</dependency>
2、解压ZIP格式的文件
public static void unzip(String srcFile,String destDirPath) {
long startTime=System.nanoTime(); //获取开始时间
try {
/** 判断文件是否存在*/
File file = new File(srcFile);
if (file.exists()) {
/** 判断文件是否是zip格式的压缩文件 */
// 获取文件的后缀
String fileSuffix = file.getName().substring(file.getName().lastIndexOf(".")); if (".zip".equals(fileSuffix)) {
net.lingala.zip4j.core.ZipFile zipFile =
new net.lingala.zip4j.core.ZipFile(srcFile);
// 设置编码格式中文设置为GBK格式
zipFile.setFileNameCharset("GBK");
// 解压压缩包
zipFile.extractAll(destDirPath);
}
}
} catch (Exception e) {
e.printStackTrace();
}
long endTime=System.nanoTime(); //获取结束时间
System.out.println("程序运行时间: "+(endTime-startTime)+"ns");
System.out.println("程序运行时间: "+(endTime-startTime)/1000000+"ms"); System.out.println("程序运行时间: "+(endTime-startTime)/1000000000+"s"); }。