相关文章推荐
痴情的皮带  ·  警惕披上“甜蜜外衣”的五彩香烟·  11 月前    · 
失望的斑马  ·  药品行指委秘书处(国家药品监督管理局高级研修 ...·  1 年前    · 
谦和的灌汤包  ·  QSqlDatabase Class | ...·  1 年前    · 
苦闷的墨镜  ·  《霞光 ...·  1 年前    · 
不拘小节的毛巾  ·  青岛:铮铮琴声民为乐 _光明网·  1 年前    · 
Code  ›  如何从java中的父zip文件中提取嵌套zip作为zip文件开发者社区
string zip
https://cloud.tencent.com/developer/ask/sof/220790
会搭讪的骆驼
2 年前
首页
学习
活动
专区
工具
TVP
返回腾讯云官网
提问
问 如何从java中的父zip文件中提取嵌套zip作为zip文件
Stack Overflow用户
提问于 2019-06-03 02:13:14
EN

我需要提取zip而不提取内部zip文件。我需要提取内部拉链,而不是爆炸。

我尝试将其作为zipinputstream读取并将其写入fileoutputstream。但我提取为zip的内部zip文件已损坏且无法读取。

ze = zis.getNextEntry();
while (ze != null) {
 if (ze.getName().endsWith(".zip") || ze.getName().endsWith(".ZIP")) {
   int len;
   while ((len = zis.read(buffer)) != -1) {
     outputStream.write(buffer, 0, len);

要重申这个要求,只提取/爆炸顶级zip并保存子zip文件。提前致谢!!

1 0 0 票数 0
EN

回答 1

Stack Overflow用户

发布于 2019-06-03 11:14:24

根据您的问题,zip文件包含一个zip文件列表。我在图片下面提供了更好的理解。

在此输入图像描述
在此输入图像描述

如果我的理解是正确的,您只对解压缩包含子zip文件列表的父zip文件感兴趣,如果这是正确的。我提供下面的代码。

import java.io.*;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
public class TestZipUtil {
  public static void unzip(String zipFilePath, String destDirectory) throws IOException {
    File srcZipFile = new File(zipFilePath);
    int size = (int) srcZipFile.length();
    byte[] bufferSize = new byte[size];
    File destDir = new File(destDirectory);
    if (!destDir.exists()) {
      destDir.mkdir();
    ZipInputStream zipIn = new ZipInputStream(new FileInputStream(zipFilePath));
    ZipEntry entry = zipIn.getNextEntry();
    while (entry != null) {
      String filePath = destDirectory + File.separator + entry.getName();
      if (!entry.isDirectory()) {
        extractFile(zipIn, filePath, bufferSize);
      } else {
        File dir = new File(filePath);
        dir.mkdir();
      zipIn.closeEntry();
      entry = zipIn.getNextEntry();
    zipIn.close();
  private static void extractFile(ZipInputStream zipIn, String filePath, byte[] bytesBuffer)
      throws IOException {
    BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(filePath));
    int read = 0;
    while ((read = zipIn.read(bytesBuffer)) != -1) {
      bos.write(bytesBuffer, 0, read);
    bos.close();
  public static void main(String[] args) throws Exception {
 
推荐文章
痴情的皮带  ·  警惕披上“甜蜜外衣”的五彩香烟
11 月前
失望的斑马  ·  药品行指委秘书处(国家药品监督管理局高级研修学院)来我校生物工程学院调研-标杆院系-生物工程学院
1 年前
谦和的灌汤包  ·  QSqlDatabase Class | Qt SQL | Qt 6.9.1
1 年前
苦闷的墨镜  ·  《霞光 《精灵世纪》主题曲(小提琴独奏)》,佚名(五线谱 小提琴谱)-弹琴吧(原蛐蛐钢琴网),小提琴谱,吉他入门,钢琴入门,优质吉他谱,钢琴谱,小提琴谱尽在弹琴吧
1 年前
不拘小节的毛巾  ·  青岛:铮铮琴声民为乐 _光明网
1 年前
今天看啥   ·   Py中国   ·   codingpro   ·   小百科   ·   link之家   ·   卧龙AI搜索
删除内容请联系邮箱 2879853325@qq.com
Code - 代码工具平台
© 2024 ~ 沪ICP备11025650号