(工具包)之zip与rar不解压获取文件

业务需求:只获取压缩包中的一级目录(包含:有文件夹跳过,rar使用的7z)

读取异常处理

/**
 * 系统异常类
 * @author sgq
 */
public class ReadZipException extends Exception {
	private static final long serialVersionUID = 1L;

    public ReadZipException(String message) {
    	super(message);
    }

    public ReadZipException(String message, Throwable ex) {
    	super(message, ex);
    }

    public ReadZipException(Throwable ex) {
    	super(ex);
    } 
}

所需依赖

<!-- https://mvnrepository.com/artifact/ant/ant -->
		<!--zip-->
		<dependency>
			<groupId>ant</groupId>
			<artifactId>ant</artifactId>
			<version>1.6.5</version>
		</dependency>

		<!-- https://mvnrepository.com/artifact/com.github.junrar/junrar --> ===》可以获取最新依赖
		<!-- 解压rar -->

		<!--<dependency>
			<groupId>com.github.junrar</groupId>
			<artifactId>junrar</artifactId>
			<version>4.0.0</version>
		</dependency>-->
				<dependency>
			<groupId>com.github.axet</groupId>
			<artifactId>java-unrar</artifactId>
			<version>1.7.0-8</version>
		</dependency>
		<dependency>
			<groupId>com.github.junrar</groupId>
			<artifactId>junrar</artifactId>
			<version>0.7</version>
		</dependency>
		<!--7z-->
		<dependency>
			<groupId>net.sf.sevenzipjbinding</groupId>
			<artifactId>sevenzipjbinding</artifactId>
			<version>16.02-2.01</version>
		</dependency>
		<dependency>
			<groupId>net.sf.sevenzipjbinding</groupId>
			<artifactId>sevenzipjbinding-all-platforms</artifactId>
			<version>16.02-2.01</version>
		</dependency>
		<--io流-->
				<dependency>
			<artifactId>commons-io</artifactId>
			<groupId>commons-io</groupId>
			<version>2.0.1</version>
		</dependency>

类文件

参考文章::点击

//导入的包
import net.sf.sevenzipjbinding.simple.ISimpleInArchive;
import net.sf.sevenzipjbinding.simple.ISimpleInArchiveItem;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import net.sf.sevenzipjbinding.*;
import net.sf.sevenzipjbinding.impl.RandomAccessFileInStream;
import java.io.*;
import java.nio.charset.Charset;
import java.util.*;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import java.util.zip.ZipInputStream;


public class ReadZipFileUtils {
//加入了log日志
  public static Logger LOG = LoggerFactory.getLogger(ReadZipFileUtils.class);
//获取压缩包中的大小和名称
    public static List<Map<String, Object>> readZipFile(String path,String appPath) throws ReadZipException {
        List<Map<String, Object>> list = new ArrayList<>();
        try {
            ZipEntry zipEntry;
            File file = new File(path);
            //判断文件是否存在,并且是zip格式的压缩包
            if (file.exists() && file.getName().toLowerCase().endsWith(".zip")) {
                //解决包内文件存在中文时的中文乱码问题
                try (ZipInputStream zipInputStream = new ZipInputStream(new FileInputStream(path), Charset.forName("GBK"))) {
                    while ((zipEntry = zipInputStream.getNextEntry()) != null) {
                        //遇到文件夹就跳过
                        if (zipEntry.isDirectory()) {
                            LOG.warn("跳过目录:{}", zipEntry.getName());
                            continue;
                        } else {
                            Map<String, Object> map = new HashMap<>();
                            //获取具体的文件名称
                            String name = zipEntry.getName();
                            //如果带有 / 则是目录不添加
                            if (name.contains("/")) {
                                LOG.warn("文件没在根目录跳过:{}", name);
                                continue;
                            }
                            //获取文件的大小
                            long size = zipEntry.getSize();
                            long csize = zipEntry.getCompressedSize();
                            map.put("filename", name);
                            map.put("size", size);
                            map.put("csize", csize);
                            list.add(map);
                        }
                    }
                }
                //判断文件是否存在,并且是rar格式的压缩包
            } else if (file.exists() && file.getName().toLowerCase().endsWith(".rar")) { //查看上传文件的后缀
                if (!file.exists()) {
                    throw new RuntimeException(file.getPath() + "所指文件不存在");
                }
                RandomAccessFile randomAccessFile =null;
                IInArchive inArchive = null;
                try {
                    randomAccessFile =new RandomAccessFile(file,"r"); //只读
                    inArchive=SevenZip.openInArchive(null,new RandomAccessFileInStream(randomAccessFile));

                    for (int idx = 0; idx < inArchive.getNumberOfItems(); idx++) {
                        if (!(boolean) (((String)inArchive.getProperty(idx, PropID.PATH)).contains("\\"))){ //过滤掉文件夹
                            if ((boolean) (((String)inArchive.getProperty(idx, PropID.PATH)).contains("."))) { //防止最后一级为空文件夹
                                Map<String, Object> map = new HashMap<>();
                                //获取具体的文件名称
                                map.put("filename", inArchive.getProperty(idx, PropID.PATH)); //文件名称
                                map.put("size", inArchive.getProperty(idx, PropID.PACKED_SIZE));//大小
                                map.put("csize", inArchive.getProperty(idx, PropID.SIZE));//原始大小
                                list.add(map);
                            }
                        } else {
                            continue;
                        }
                    }
                }
                finally {
                    randomAccessFile.close();
                }
            }
        } catch (Exception e) {
            throw new ReadZipException("读取压缩文件错误:" + e.getMessage(), e);
        }
        return list;
    }
}
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

大众筹码

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值