java.util.zip.ZipException: only DEFLATED entries can have EXT descriptor

1、使用jdk自带的解压报错java.util.zip.ZipException: only DEFLATED entries can have EXT descriptor
2、解决方案使用apache的ant解压
org.apache.tools.zip
3、引入pom

    <!-- https://mvnrepository.com/artifact/org.apache.ant/ant -->
    <dependency>
      <groupId>org.apache.ant</groupId>
      <artifactId>ant</artifactId>
      <version>1.10.5</version>
    </dependency>

import com.weihui.member.gateway.enums.ErrorCode;
import com.weihui.member.gateway.exception.BizException;
import org.apache.tools.zip.ZipEntry;
import org.apache.tools.zip.ZipFile;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Enumeration;

/**
 * 
 */
public class ZipUtilApache {
    private static final Logger log  = LoggerFactory.getLogger(ZipUtilApache.class);
    private static final int buffer = 2048;

    /**
     * 解压Zip文件
     *
     * @param path 文件目录
     */
    public static void unZip(String path) {
        int count = -1;
        String savepath = "";
        File file = null;
        InputStream is = null;
        FileOutputStream fos = null;
        BufferedOutputStream bos = null;
        savepath = new File(path).getParent() + File.separator; //保存解压文件目录
        new File(savepath).mkdir(); //创建保存目录
        ZipFile zipFile = null;
        try {
            zipFile = new ZipFile(path, "gbk"); //解决中文乱码问题
            Enumeration<?> entries = zipFile.getEntries();
            while (entries.hasMoreElements()) {
                byte buf[] = new byte[buffer];
                ZipEntry entry = (ZipEntry) entries.nextElement();
                String filename = entry.getName();
                boolean ismkdir = false;
                if (filename.lastIndexOf("/") != -1) { //检查此文件是否带有文件夹
                    ismkdir = true;
                }
                filename = savepath + filename;
                if (entry.isDirectory()) { //如果是文件夹先创建
                    file = new File(filename);
                    file.mkdirs();
                    continue;
                }
                file = new File(filename);
                if (!file.exists()) { //如果是目录先创建
                    if (ismkdir) {
                        new File(filename.substring(0, filename.lastIndexOf("/"))).mkdirs(); //目录先创建
                    }
                }
                file.createNewFile(); //创建文件
                is = zipFile.getInputStream(entry);
                fos = new FileOutputStream(file);
                bos = new BufferedOutputStream(fos, buffer);
                while ((count = is.read(buf)) > -1) {
                    bos.write(buf, 0, count);
                }
                bos.flush();
                bos.close();
                fos.close();
                is.close();
            }
            zipFile.close();
        } catch (IOException ioe) {
            log.error("ZipUtilApache-unZip-IOException",ioe);
            throw new BizException(ErrorCode.SYSTEM_ERROR,"解压缩商户资质文件["+path+"]失败","解压缩商户资质文件["+path+"]失败");
        } finally {
            try {
                if (bos != null) {
                    bos.close();
                }
                if (fos != null) {
                    fos.close();
                }
                if (is != null) {
                    is.close();
                }
                if (zipFile != null) {
                    zipFile.close();
                }
            } catch (Exception e) {
                log.error("ZipUtilApache-unZip-Exception",e);
            }
        }
    }

    public static void main(String[] args) {
        unZip("E:\\下载\\mgs1\\test\\GI20210303173812PXPKRV2304.zip");
        // unZip("E:\\下载\\mgs1\\test\\47kong.zip");
    }
}


下面是出现报错的

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream;
	/**
	 * 解压指定zip文件至当前文件
	 * @param unZipfileName  需要解压的zip文件名
	 */
	public void unZip(String unZipfileName){
		FileOutputStream fileOut = null;
		File file;

		try{
			this.zipIn = new ZipInputStream(new BufferedInputStream(new FileInputStream(unZipfileName)));

			while((this.zipEntry = this.zipIn.getNextEntry()) != null){

				file = new File(new File(unZipfileName).getParent()+File.separator+this.zipEntry.getName());

				if(this.zipEntry.isDirectory()){
					file.mkdirs();
				} else {
					//如果指定文件的目录不存在,则创建之.
					File parent = file.getParentFile();
					if(!parent.exists()){
						parent.mkdirs();
					}

					fileOut = new FileOutputStream(file);
					while(( this.readedBytes = this.zipIn.read(this.buf) ) > 0){
						fileOut.write(this.buf , 0 , this.readedBytes );
					}
					
					if(fileOut != null) {
		                try {
		                    fileOut.close();
		                } catch (IOException e) {
		                    e.printStackTrace();
		                }
		            }
				}
				this.zipIn.closeEntry();
			}
		}catch(Exception ioe){
		    ioe.printStackTrace();

		} finally {
			if(this.zipIn != null) {
				try {
					this.zipIn.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			if(fileOut != null) {
				try {
					fileOut.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
### 回答1: java.util.zip.ZipException: zip file is empty 是一个Java中的异常错误。这个错误通常是因为试图读取一个空的ZIP文件而导致的。 ZIP文件是一种压缩文件格式,通常用于将多个文件压缩成一个文件,以方便存储和传输。当我们使用Javajava.util.zip包中的类来操作ZIP文件时,有时会遇到这个异常。 这个异常的原因通常有两个可能性。第一种可能是我们试图打开一个不存在的或者不可读的ZIP文件,这种情况下需要确认文件的路径和权限是否正确。第二种可能是我们试图打开一个空的ZIP文件,也就是ZIP文件中没有任何的压缩文件数据。这种情况下,我们需要检查ZIP文件的内容,确保其中包含了我们需要的压缩文件。 要解决这个问题,我们可以先检查ZIP文件的路径和权限是否正确。如果确定路径和权限没有问题,那么我们可以通过查看ZIP文件的内容来确定是否为空。如果ZIP文件确实为空,我们需要重新创建一个有效的ZIP文件,并将需要压缩的文件添加到这个新的ZIP文件中。 如果我们使用的是第三方库或工具来操作ZIP文件,那么我们还需要查看其文档或者官方网站,以了解更多关于这个异常的具体原因和解决方法。 总之,java.util.zip.ZipException: zip file is empty 是一个表示ZIP文件为空的异常。要解决这个问题,我们可以先确认ZIP文件的路径和权限是否正确,然后检查ZIP文件的内容,确保其中包含了需要的压缩文件。 ### 回答2: java.util.zip.ZipException: zip文件为空。这个错误通常是由于尝试操作一个空的ZIP文件而引起的。ZIP文件是一种压缩文件格式,它可以存储一个或多个文件文件夹。 可能的原因之一是在尝试读取或写入ZIP文件时,ZIP文件的内容为空。这可能是因为创建ZIP文件的过程中出现了错误,或者在读取ZIP文件之前,文件被意外地删除或清空了。 要解决这个问题,我们可以首先确保ZIP文件存在,并且大小不为零。如果ZIP文件确实存在但为空,我们可以检查是否存在任何错误或异常,以防止ZIP文件内容丢失。 另一种情况是在使用JavaZipInputStream或ZipOutputStream类处理ZIP文件时会出现这个错误。在这种情况下,我们可以检查代码中是否存在逻辑错误或错误的文件路径,例如尝试读取或写入不存在的ZIP文件。 总之,当遇到"java.util.zip.ZipException: zip file is empty"错误时,我们应该首先检查ZIP文件是否为空,然后排除任何可能导致ZIP文件空的问题。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值