从ZipInputStream中读取文件的内容

  在写一个接口的时候,调用方只提供一个 ZipInputStream, 需要解析出里面的所有的内容,思路就是按位读取流的字节信息:

  以下是示例代码:

    private static String getFileContentStrFromStream(Long fileSize, ZipInputStream zipStream) throws Exception {
        String fileContentStr = "";
        byte[] buffer = new byte[fileSize.intValue()];
        byte[] tmp = new byte[1024];
        int len = 0;
        int off = 0;
        while((len = zipStream.read(bs)) != -1){
            System.arraycopy(tmp, 0, buffer, off, len);
            off += len;
        }
        fileContentStr = new String(buffer, "UTF-8");

        return fileContentStr;

    }

    public static void execute(ZipInputStream zipStream) throws Exception{
        ZipEntry zipEntry = null;
        while ((zipEntry = zipStream.getNextEntry()) != null) {
            Long fileSize = zipEntry.getSize();
            String fileContentStr = getFileContentStrFromStream(fileSize, zipStream);
            System.out.println(fileContentStr);
        }
    }

发表评论

This site uses Akismet to reduce spam. Learn how your comment data is processed.