Write ZipEntry Data To String

前端 未结 4 1549
你的背包
你的背包 2021-02-05 20:16

I have retrieved a zip entry from a zip file like so.

InputStream input = params[0];
ZipInputStream zis = new ZipInputStream(input);

ZipEntry entry;
try {
    w         


        
4条回答
  •  心在旅途
    2021-02-05 21:11

    With set encoding (UTF-8) and without creation of strings:

    import java.util.zip.ZipInputStream;
    import java.util.zip.ZipEntry;
    import java.io.ByteArrayOutputStream;
    import static java.nio.charset.StandardCharsets.UTF_8;
    String charset = "UTF-8";
    
    try (
      ZipInputStream zis = new ZipInputStream(input, UTF_8); 
      ByteArrayOutputStream baos = new ByteArrayOutputStream()
    ) {
      byte[] buffer = new byte[1024];
      int read = 0;
      ZipEntry entry;
      while ((entry = zis.getNextEntry()) != null)
        while ((read = zis.read(buffer, 0, buffer.length)) > 0)
          baos.write(buffer, 0, read);
      String content = baos.toString(charset);
    }
    

提交回复
热议问题