How can I convert byte array to ZIP file

前端 未结 2 1650
醉话见心
醉话见心 2020-12-09 11:44

I am trying to convert an array of bytes into a ZIP file. I got bytes using the following code:

byte[] originalContentBytes= new Verification().readBytesFro         


        
相关标签:
2条回答
  • 2020-12-09 12:25

    You probably are looking for code like this:

    ZipInputStream z = new ZipInputStream(new ByteArrayInputStream(buffer))
    

    now you can get the zip file contents via getNextEntry()

    0 讨论(0)
  • 2020-12-09 12:37

    To get the contents from the bytes you can use

    ZipInputStream zipStream = new ZipInputStream(new ByteArrayInputStream(bytes));
    ZipEntry entry = null;
    while ((entry = zipStream.getNextEntry()) != null) {
    
        String entryName = entry.getName();
    
        FileOutputStream out = new FileOutputStream(entryName);
    
        byte[] byteBuff = new byte[4096];
        int bytesRead = 0;
        while ((bytesRead = zipStream.read(byteBuff)) != -1)
        {
            out.write(byteBuff, 0, bytesRead);
        }
    
        out.close();
        zipStream.closeEntry();
    }
    zipStream.close(); 
    
    0 讨论(0)
提交回复
热议问题