java 操作zip文件

这段代码展示了如何读取本地zip文件,从中提取包含特定字符串(如drawable-xxhdpi)的文件,然后将这些文件存入新的zip文件。此外,还使用了RestTemplate从URL获取zip文件,处理后存储到OSS,并提供了重新上传处理后zip文件的功能。
摘要由CSDN通过智能技术生成

读取本地zip文件,并将文件中部分文件取出存为新的zip文件 

    public static void main(String[] args) throws Exception{
        BufferedInputStream bufferedInputStream = new BufferedInputStream(new FileInputStream("D:\\data\\tmp\\theme_apk.zip"));
        String outFilePath = "D:\\data\\tmp\\sub.zip";
        ZipInputStream zis = new ZipInputStream(bufferedInputStream);
        ZipEntry zipEntry;
        byte[] buffer = new byte[1024];
        BufferedReader br = new BufferedReader( new InputStreamReader(zis));
        ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(outFilePath));

        while((zipEntry = zis.getNextEntry())!=null){
            System.out.println("zip name: "  + zipEntry.getName());
            if( zipEntry.getName().contains("drawable-xxhdpi") ){
                zos.putNextEntry(new ZipEntry(zipEntry.getName().substring(22)));
                int len = -1;
                while ((len=zis.read(buffer)) != -1){
                    zos.write(buffer, 0, len);
                }
            }
            zipEntry = null;
        }
        zos.close();
        zis.closeEntry();
        zis.close();

    }



    public void refreshFile() throws  Exception{
        RestTemplate restTemplate = new RestTemplate();
        String path = "https://cdn-theme-eu-central-1.shalltry.com/rlktheme/theme/1680003127198.theme";
        URI uri = new URI(path);
        ResponseEntity<org.springframework.core.io.Resource> forEntity = restTemplate.getForEntity(path, org.springframework.core.io.Resource.class);
        ZipInputStream zis = new ZipInputStream(forEntity.getBody().getInputStream());

        String outFilePath = "D:\\data\\tmp\\test.zip";
        ZipEntry zipEntry;
        byte[] buffer = new byte[1024];
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        ZipOutputStream zos = new ZipOutputStream(byteArrayOutputStream);
        while((zipEntry = zis.getNextEntry())!=null){
//            System.out.println("zip name: "  + zipEntry.getName());
            if( zipEntry.getName().contains("drawable-xxhdpi") ){
                zos.putNextEntry(new ZipEntry(zipEntry.getName().substring(22)));
                int len = -1;
                while ((len=zis.read(buffer)) != -1){
                    zos.write(buffer, 0, len);
                }
                zos.closeEntry();
                zos.flush();
            }
            zipEntry = null;
        }

        zis.close();
        zos.flush();
        byteArrayOutputStream.close();
        zos.close();

//存储到OSS
        System.out.println("size : " + byteArrayOutputStream.size());
        Map<String, Object> result = fileManagerServer.fileUpload("dev",  new ByteArrayInputStream(byteArrayOutputStream.toByteArray()), "text/plain", "test.zip");
        System.out.println(result.get("url"));

//存储到本地文件
/*        InputStreamSource inputStreamSource = new ByteArrayResource(byteArrayOutputStream.toByteArray());
        ZipInputStream zipInputStream = new ZipInputStream(inputStreamSource.getInputStream());

        FileOutputStream fos = new FileOutputStream(outFilePath);
        ZipOutputStream zipOutputStream = new ZipOutputStream(fos);
        byte[] bytes = new byte[1024];
        int len = -1;
        while((zipEntry = zipInputStream.getNextEntry()) != null){

            zipOutputStream.putNextEntry(new ZipEntry(zipEntry.getName()));
            while((len= zipInputStream.read(bytes)) !=-1){
                zipOutputStream.write(bytes, 0, len);
            }
            zipOutputStream.closeEntry();
            zipOutputStream.flush();
        }
        
        zipInputStream.close();
        zipOutputStream.close();
        fos.close();*/


    }



    public static void reUpload() throws Exception {
        FileItemFactory factory = new DiskFileItemFactory(16,null);
        String textFieldName = "file";
        FileItem item = factory.createItem(textFieldName, MediaType.MULTIPART_FORM_DATA_VALUE,true,"fileName");

        RestTemplate restTemplate = new RestTemplate();
        String path = "https://cdn-theme-eu-central-1.shalltry.com/rlktheme/theme/1680003127198.theme";
        URI uri = new URI(path);
        ResponseEntity<org.springframework.core.io.Resource> forEntity = restTemplate.getForEntity(path, org.springframework.core.io.Resource.class);
        ZipInputStream zis = new ZipInputStream(forEntity.getBody().getInputStream());

        String outFilePath = "D:\\data\\tmp\\test.zip";
        ZipEntry zipEntry;
        byte[] buffer = new byte[102400000];
        ZipOutputStream zos = new ZipOutputStream(item.getOutputStream());
        while((zipEntry = zis.getNextEntry())!=null){
            System.out.println("zip name: "  + zipEntry.getName());
            if( zipEntry.getName().contains("drawable-xxhdpi") ){
                zos.putNextEntry(new ZipEntry(zipEntry.getName().substring(22)));
                int len = -1;
                while ((len=zis.read(buffer)) != -1){
                    zos.write(buffer, 0, len);
                }
            }
            zipEntry = null;
        }
        zos.flush();

        FileOutputStream fos = new FileOutputStream(new File(outFilePath));
        DataInputStream dis = new DataInputStream(item.getInputStream());
        byte[] bytes = new byte[1024];
        int len = -1;
        while((len= dis.read(bytes))!=-1){
            fos.write(bytes, 0, len);
        }

        fos.close();
        dis.close();
        zos.close();
        zis.closeEntry();
        zis.close();

    }

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值