文章目录

  • 场景
  • 分析
  • 解决

场景

使用ZipEntry解压zip文件报错:  java.lang.IllegalArgumentException: MALFORMED  

分析

压缩文件中含有中文, 在ZipCoder调用toString函数报错。此处识别成了utf8,
所以 在解压时设置成gbk字符集就行了
String toString(byte[] ba, int length) {CharsetDecoder cd = decoder().reset();int len = (int)(length * cd.maxCharsPerByte());char[] ca = new char[len];if (len == 0)return new String(ca);// UTF-8 only for now. Other ArrayDeocder only handles// CodingErrorAction.REPLACE mode. ZipCoder uses// REPORT mode.// 这里是报错位置if (isUTF8 && cd instanceof ArrayDecoder) {int clen = ((ArrayDecoder)cd).decode(ba, 0, length, ca);if (clen == -1)    // malformedthrow new IllegalArgumentException("MALFORMED");return new String(ca, 0, clen);}ByteBuffer bb = ByteBuffer.wrap(ba, 0, length);CharBuffer cb = CharBuffer.wrap(ca);CoderResult cr = cd.decode(bb, cb, true);if (!cr.isUnderflow())throw new IllegalArgumentException(cr.toString());cr = cd.flush(cb);if (!cr.isUnderflow())throw new IllegalArgumentException(cr.toString());return new String(ca, 0, cb.position());}

解决

ZipFile zf = new ZipFile(file, Charset.forName(“gbk”));

    /*** 解压文件* */@Overridepublic void unZip(String srcPath, String dest) throws IOException {File file = new File(srcPath);if (!file.exists()) {throw new RuntimeException(srcPath + "所指文件不存在");}ZipFile zf = new ZipFile(file, Charset.forName("gbk"));Enumeration entries = zf.entries();ZipEntry entry = null;while (entries.hasMoreElements()) {entry = (ZipEntry) entries.nextElement();System.out.println("解压" + entry.getName());if (entry.isDirectory()) {String dirPath = dest + File.separator + entry.getName();Path path = Paths.get(dirPath);Files.createDirectories(path);} else {// 表示文件File f = new File(dest + File.separator + entry.getName());if (!f.exists()) {String dirs = f.getParent();Path path = Paths.get(dirs);Files.createDirectories(path);}f.createNewFile();// 将压缩文件内容写入到这个文件中InputStream is = zf.getInputStream(entry);FileOutputStream fos = new FileOutputStream(f);int count;byte[] buf = new byte[8192];while ((count = is.read(buf)) != -1) {fos.write(buf, 0, count);}is.close();fos.close();}}}

使用ZipEntry解压zip文件报错: java.lang.IllegalArgumentException: MALFORMED相关推荐

  1. 如何解决“Linux解压zip文件报错‘-bash: unzip: 未找到命令’”?

    一.报错:  二.问题原因: 没有安装zip 没有安装unzip 三. 解决方法: 1.安装zip yum install zip 2.安装unzip yum install unzip  再试一下:

  2. linux文件解压zip文件,linux下解压zip文件报错

    今天在Linux上解压zip文件报错,报错信息如下: Archive: abc.zip End-of-central-directory signature not found. Either thi ...

  3. 报错java.lang.IllegalArgumentException: Result Maps collection does not contain value解决

    使用mybatis报错 java.lang.IllegalArgumentException: Result Maps collection does not contain value for co ...

  4. 客户端启动报错java.lang.IllegalArgumentException: no server available的解决方案 SpringCloud中 Nacos做注册中心

    客户端启动报错java.lang.IllegalArgumentException: no server available的解决方案 SpringCloud中 Nacos做注册中心(谷粒) 报错内容 ...

  5. Cookie报错java.lang.IllegalArgumentException

    项目中做自动登录和保存密码时,Cookie报错Java.lang.IllegalArgumentException,上google查了下 在http://hi.baidu.com/xtxycy/blo ...

  6. 解压.tar 文件报错

    解压test.tar 文件报错 gzip: stdin: not in gzip format tar: Child returned status 1 tar: Error is not recov ...

  7. myeclipse 修改项目名Tomcat启动项目报错java.lang.IllegalArgumentException: Can't convert argument: null...

    一.Tomcat启动报错: 有时候我们会避免项目重名,在导入项目时会修改项目名称. 修改项目名称后启动该项目至tomcat,报错:java.lang.IllegalArgumentException: ...

  8. 报错: java.lang.IllegalArgumentException: mapper [categoryName] of different type, current_type [text]

    Elasticsearch报错: 非法参数异常 Caused by: java.lang.IllegalArgumentException: mapper [categoryName] of diff ...

  9. 百度地图使用模拟器报错java.lang.IllegalArgumentException: No config chosen

    在使用百度地图开发时,使用手机没事儿,但是使用到模拟器就开始报错:java.lang.IllegalArgumentException: No config chosen 原因分析: 在后台打印的错误 ...

  10. Nacos发布配置 项目报错java.lang.IllegalArgumentException

    环境: Java1.8 Nacos1.2.1 Spring Cloud Hoxton.SR3 Spring Cloud Alibaba 2.2.1.RELEASE 项目正在运行,使用Nacos发布配置 ...

最新文章

  1. 什么是php model类,thinkphp的自定义model类有什么作用?
  2. SSH Secure File Transfer上传文件错误:encountered 1 errors during the transfer解决办法
  3. 计算机原理中的cps,信息物理融合系统(CPS)原理
  4. hdu 1806 Frequent values 线段树
  5. 阿里巴巴开源轻量级深度神经网络推理引擎MNN
  6. python循环小甲鱼教程_python之 小甲鱼教程 Easygui 篇
  7. [MySql]默认密码的查找与修改
  8. 第十二届蓝桥杯模拟赛Python组(第一期)
  9. python下载安装教程-Python下载并安装图形教程[超级详细]
  10. php获取笔顺矢量,php如何获取汉字笔画数功能的实例分析
  11. Python实现简单命令行版《中国象棋》不使用第三方库
  12. 用数据分析看共享单车
  13. mysql 父子关系查询,父子关系-SQL查询
  14. VC编程——QQ连连看外挂
  15. 【JAVA作业】异常、随机数、List
  16. Winform常用控件的使用(一)
  17. 多媒体基础学习笔记:MPEG-7
  18. 基于MATLAB APP Designer 和 Python 的音视频剪辑App_02
  19. VISUAL STUDIO入门使用教程
  20. Python 3 字符串 format( ) 方法

热门文章

  1. WIFI实践入门--基本命令--iwconfig
  2. 大数据第三季--zk(day1)-徐培成-专题视频课程
  3. 【PyTorch实战】用PyTorch实现基于神经网络的图像风格迁移
  4. 解读《美国国家BIM标准》 – BIM能力成熟度模型 (七)
  5. CV520直接pin对pin替换 MS520,直接替换,无需修改硬件以及软件
  6. JAVA基础面试题——继承
  7. 计算机思维导图ppt模板,经典PPT模版(含各类思维导图).ppt
  8. vue 响应式布局组件_今天如何使用响应式Web组件
  9. [FAQ03776] [Power]关于RTC唤醒系统问题
  10. java中递归遍历文件夹下所有文件