无法读取ZIpInputStream

时间:2018-06-12 16:03:54

标签: java

我在将ZipInputStream读取到字节数组时遇到了一些问题 - 结果为-1。但是我可以将其作为文件流阅读,因此我不确定我是否遗漏了某些内容:

public void readContent () throws IOException {
           int size = 2048;
           byte[] file = new byte[size];
           byte[] stream = new byte[size];
           FileInputStream fileStream = new FileInputStream(this.file);
           ZipInputStream zipStream = new ZipInputStream(fileStream);;
           System.out.println("Reading as a file stream " + fileStream.read(file,0,size)); //output 2048
           System.out.println("Reading as a zip stream " + zipStream.read(stream,0,size)); // output -1
          }

1 个答案:

答案 0 :(得分:0)

来自ZipInputStream的数据应通过ZipEntry阅读。这是如何从给定的InputStream读取zip存档并使用密钥检索Map的示例 - 完整的zip条目路径,值 - zip条目数据:

public class ZipUtils {

    private static final Comparator<String> COMPARATOR_STRING_DESC = (str1, str2) -> str1 == null ? 1 : str2 == null ? -1 : -str1.compareTo(str2);

    public static Map<String, byte[]> unzipIt(InputStream is) throws IOException {
        try (ZipInputStream in = new ZipInputStream(is)) {
            ZipEntry entry;
            Map<String, byte[]> content = new HashMap<>();
            Set<String> dirs = new TreeSet<>(COMPARATOR_STRING_DESC);

            while ((entry = in.getNextEntry()) != null) {
                String path = removeDirectoryMarker(replaceIncorrectFileSeparators(entry.getName()));

                if (isDirectory(entry)) {
                    dirs.add(path);
                } else {
                    content.put(path, IOUtils.toByteArray(in));
                }
            }

            addOnlyEmptyDirectories(dirs, content);

            return content.isEmpty() ? Collections.emptyMap() : content;
        }
    }

    private static boolean isDirectory(ZipEntry entry) {
        return entry.isDirectory() || entry.getName().endsWith(ILLEGAL_DIR_MARKER);
    }

    private static void addOnlyEmptyDirectories(Set<String> dirs, Map<String, byte[]> content) {
        if (dirs.isEmpty()) {
            return;
        }

        Set<String> paths = new HashSet<>(content.keySet());

        for (String dir : dirs) {
            boolean empty = true;

            for (String path : paths) {
                if (path.startsWith(dir)) {
                    empty = false;
                    break;
                }
            }

            if (empty) {
                content.put(dir, null);
            }
        }
    }

    private static final String DIR_MARKER = "/";
    private static final String ILLEGAL_DIR_MARKER = "\\";
    private static final java.util.regex.Pattern BACK_SLASH = Pattern.compile("\\\\");

    private static String removeDirectoryMarker(String path) {
        return path.endsWith(DIR_MARKER) || path.endsWith(ILLEGAL_DIR_MARKER) ? path.substring(0, path.length() - 1) : path;
    }

    private static String replaceIncorrectFileSeparators(String path) {
        return BACK_SLASH.matcher(path).replaceAll(DIR_MARKER);
    }
}