2种解压zip并读取流的方式

private void threadToZip(MultipartFile file) {

        new Thread() {
            public void run() {
                ZipInputStream zipInputStream = null;
                ByteArrayOutputStream arrayOutputStream = null;
                try {
                    zipInputStream = new ZipInputStream(file.getInputStream(), Charset.forName("GBK"));
                    for(ZipEntry zipEntry; (zipEntry = zipInputStream.getNextEntry()) != null;){
                        if(!zipEntry.isDirectory()){
                            byte[] buffer = new byte[1024];
                            arrayOutputStream=new ByteArrayOutputStream();
                            for(int i;(i=zipInputStream.read(buffer)) > -1;){
                                arrayOutputStream.write(buffer,0,i);
                            }
                            arrayOutputStream.flush();
                            // 关闭当前zip条目并准备读取下一个条目
                            zipInputStream.closeEntry();
                            // 读入构造器:文件流,读取成哪个Java对象,读取监听器
                            ExcelReader excelReader = EasyExcel.read(new ByteArrayInputStream(arrayOutputStream.toByteArray()), ComplaintWorkOrders.class, easyExcelListener)
                                    .excelType(ExcelTypeEnum.CSV).registerConverter(new CustomConverter())
                                    .charset(CharsetUtil.CHARSET_GBK)
                                    .build();
                            ReadSheet readSheet = EasyExcel.readSheet(0).build();
                            excelReader.read(readSheet);
                            excelReader.finish();
                        }
                    }
                } catch (Exception e) {

                }finally {
                    IOUtils.close(arrayOutputStream);
                    IOUtils.close(zipInputStream);
                }
            }
        }.start();
    }

 2

    private void threadToZip(MultipartFile file) {

        new Thread() {
            public void run() {
                InputStream inputStream = null;
                FileOutputStream fileOutputStream = null;
                InputStream zipInputStream = null;
                try {
                    inputStream = file.getInputStream();
                    File newFile = new File(file.getOriginalFilename()+System.currentTimeMillis());
                    fileOutputStream = new FileOutputStream(newFile);
                    byte[] buffer = new byte[1024];
                    for(int bytesRead ;(bytesRead = inputStream.read(buffer,0,1024)) != -1;){
                        fileOutputStream.write(buffer,0,bytesRead);
                    }
                    fileOutputStream.flush();
                    ZipFile zipFile = new ZipFile(newFile);
                    for(Enumeration<? extends  ZipEntry> e = zipFile.entries();e.hasMoreElements();){
                        zipInputStream = zipFile.getInputStream(e.nextElement());
                        // 读入构造器:文件流,读取成哪个Java对象,读取监听器
                        ExcelReader excelReader = EasyExcel.read(zipInputStream, ComplaintWorkOrders.class, easyExcelListener)
                                .excelType(ExcelTypeEnum.CSV).registerConverter(new CustomConverter())
                                .charset(CharsetUtil.CHARSET_GBK)
                                .build();
                        ReadSheet readSheet = EasyExcel.readSheet(0).build();
                        excelReader.read(readSheet);
                        excelReader.finish();
                    }
                    //删除临时文件
                    newFile.delete();

                } catch (Exception e) {

                }finally {
                    IOUtils.close(zipInputStream);
                    IOUtils.close(fileOutputStream);
                    IOUtils.close(inputStream);
                }
            }
        }.start();
    }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
你可以使用Javajava.util.zip包来实现解压zip文件并读取其内容。下面是一个示例代码: ```java import java.io.BufferedInputStream; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.util.zip.ZipEntry; import java.util.zip.ZipInputStream; public class UnzipExample { public static void main(String[] args) { String zipFilePath = "path/to/your/zip/file.zip"; String destinationFolder = "path/to/destination/folder"; try { unzip(zipFilePath, destinationFolder); readContents(destinationFolder); } catch (IOException e) { e.printStackTrace(); } } public static void unzip(String zipFilePath, String destinationFolder) throws IOException { byte[] buffer = new byte[1024]; File folder = new File(destinationFolder); if (!folder.exists()) { folder.mkdirs(); } try (ZipInputStream zipInputStream = new ZipInputStream(new BufferedInputStream(new FileInputStream(zipFilePath)))) { ZipEntry zipEntry; while ((zipEntry = zipInputStream.getNextEntry()) != null) { String entryName = zipEntry.getName(); File entryFile = new File(destinationFolder + File.separator + entryName); if (entryFile.getParentFile() != null && !entryFile.getParentFile().exists()) { entryFile.getParentFile().mkdirs(); } if (!zipEntry.isDirectory()) { int bytesRead; while ((bytesRead = zipInputStream.read(buffer)) != -1) { entryFile.createNewFile(); FileOutputStream fileOutputStream = new FileOutputStream(entryFile); fileOutputStream.write(buffer, 0, bytesRead); fileOutputStream.close(); } } else { entryFile.mkdirs(); } zipInputStream.closeEntry(); } } } public static void readContents(String folderPath) { File folder = new File(folderPath); if (folder.exists() && folder.isDirectory()) { File[] files = folder.listFiles(); if (files != null) { for (File file : files) { if (file.isDirectory()) { System.out.println("Directory: " + file.getName()); } else { System.out.println("File: " + file.getName()); } } } } } } ``` 在上述示例代码中,你需要替换`zipFilePath`和`destinationFolder`变量的值为你实际的zip文件路径和解压后的目标文件夹路径。`unzip`方法负责解压zip文件,`readContents`方法用于读取解压后的文件夹内容并打印。 请确保你已经正确配置了Java环境,并导入了必要的类和包。运行示例代码后,它将解压zip文件并打印出解压后的文件和文件夹列表。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值