zip文件解压缩问题:no current ZIP entry ::: MALFORMED

今天写一个解压缩文件的小程序,各种问题,于是各种百度,各种找书最终得以解决

zip文件解压缩问题:no current ZIP entry    :::    MALFORMED

解压含有中文问题:

 

java.lang.IllegalArgumentException: MALFORMED
	at java.util.zip.ZipCoder.toString(Unknown Source)
	at java.util.zip.ZipFile.getZipEntry(Unknown Source)
	at java.util.zip.ZipFile.access$900(Unknown Source)
	at java.util.zip.ZipFile$1.nextElement(Unknown Source)
	at java.util.zip.ZipFile$1.nextElement(Unknown Source)
	at com.cn.zip.ZipFileTest.unZip(ZipFileTest.java:50)
	at com.cn.zip.ZipFileTest.main(ZipFileTest.java:29)

 

 

 

解决方式:

//最后实践可以解决压缩中文的问题,不过支持JDK1.7及以上
ZipFile zip = new ZipFile(fileName,Charset.forName("gbk"));
 

 

 

 

压缩问题:

 

java.util.zip.ZipException: no current ZIP entry
	at java.util.zip.ZipOutputStream.write(Unknown Source)
	at com.cn.zip.ZipFileTest.zip(ZipFileTest.java:170)
	at com.cn.zip.ZipFileTest.zip(ZipFileTest.java:161)
	at com.cn.zip.ZipFileTest.zip(ZipFileTest.java:161)
	at com.cn.zip.ZipFileTest.zip(ZipFileTest.java:128)
	at com.cn.zip.ZipFileTest.main(ZipFileTest.java:30)

 

代码:

  
public static void zip(ZipOutputStream out, String basePath, File file) {
        try {
            if (file.isDirectory()) {// 如果是目录
                File[] files = file.listFiles();
                if (files.length == 0) {
                    // 将文件夹放入zip中
                    out.putNextEntry(new ZipEntry(basePath + "/"));
                    out.closeEntry();
                    // 关闭zip文件中之前打开的项
                }
                for (File data : files) {
                    zip(out, basePath + "/" + data.getName(), data);
                }
            } else {
                FileInputStream in = new FileInputStream(file);
                BufferedInputStream bis = new BufferedInputStream(in);
                byte[] buf = new byte[1024];
                //1 out.putNextEntry(new ZipEntry(basePath));// 将文件夹放入zip中
                int bat;
                while ((bat = bis.read(buf)) != -1) {
                    out.write(buf, 0, bat); // 将字节流写入当前zip目录
                }
                //2 out.closeEntry();// 关闭zip文件中之前打开的项
                if (bis != null) {
                    bis.close();
                }
                if (in != null) {
                    in.close();
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

 

 

解决:
最终加上注释1、2的地方便可以了。

 

 

其中在写入文件的时候当我用BufferedOutPutStream进行写入时写入进压缩包中图片有大小,可是大部分图片都打不开,正在找原因。。

 


 

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值