Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/shell/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 读取zip文件需要获取纯文本而不是二进制流_Java - Fatal编程技术网

Java 读取zip文件需要获取纯文本而不是二进制流

Java 读取zip文件需要获取纯文本而不是二进制流,java,Java,我已经将二进制流写入zip文件。当我解压时,我看到了二进制字符。 请帮助我如何获取纯文本 Read方法-问题我需要获取保存的纯文本,而不是二进制流 public static void readzipBytes(String filePath){ try { ZipFile zf = new ZipFile(filePath); Enumeration entries = zf.entries(); while (entr

我已经将二进制流写入zip文件。当我解压时,我看到了二进制字符。 请帮助我如何获取纯文本

Read方法-问题我需要获取保存的纯文本,而不是二进制流

 public static void readzipBytes(String filePath){
    try {
          ZipFile zf = new ZipFile(filePath);
          Enumeration entries = zf.entries();


          while (entries.hasMoreElements()) {
            ZipEntry ze = (ZipEntry) entries.nextElement();



              long size = ze.getSize();
              if (size > 0) {
                System.out.println("Length is " + size);
                BufferedReader br = new BufferedReader(
                    new InputStreamReader(zf.getInputStream(ze)));
                String line;
                while ((line = br.readLine()) != null) {
                  System.out.println(line);

                  byte[] bytearray =  line.getBytes("UTF-8");
                  System.out.println(">>>>>>>>>>>>>>>>>>>>>>>"+bytearray.length);
                  for(int o=0; o<bytearray.length; o++){
                    System.out.println("SSS=="+bytearray[o]);

                }

                  String str = new String(bytearray);
                }
                br.close();
              }

          }
        } catch (IOException e) {
          e.printStackTrace();
        }
}
保存方法

 public static byte[] savezipBytesWithWriter(String filename, String message) throws IOException {
        byte[] input = message.getBytes();
        System.out.println("intput length"+input.length);
        File f = new File("C:\\tmp\\t113.zip");
        OutputStream outputStream = new FileOutputStream (f); 
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        ZipOutputStream zos = new ZipOutputStream(baos);
        Writer writer = new OutputStreamWriter(baos);
        writer.write(message);


        ZipEntry entry = new ZipEntry(filename);
        entry.setSize(input.length);
        zos.putNextEntry(entry);

        System.out.println("baos barray length"+baos.toByteArray().length);           
        zos.write(baos.toByteArray());
        zos.closeEntry();
        zos.close();
        System.out.println(baos.toString());
        baos.writeTo(outputStream);
        return baos.toByteArray();
    }
ZipOutputStream zos = new ZipOutputStream(baos);;
这里,您正在围绕一个
ByteArrayOutputStream构建一个
ZipoutStream

Writer writer = new OutputStreamWriter(baos);
writer.write(message);
zos.write(baos.toByteArray());
在这里,您正在围绕相同的
ByteArrayOutputStream构建一个
编写器。

Writer writer = new OutputStreamWriter(baos);
writer.write(message);
zos.write(baos.toByteArray());
在这里,您将通过tearrayoutputstream直接向
写入消息。

Writer writer = new OutputStreamWriter(baos);
writer.write(message);
zos.write(baos.toByteArray());
这里,您正在将
ZipOutputStream
环绕的
ByteArrayOutputStream
字节数组的内容写入
zipoutstream。

Writer writer = new OutputStreamWriter(baos);
writer.write(message);
zos.write(baos.toByteArray());
基本上,这段代码没有意义


尝试将消息直接写入
zipoutput流。
您根本不需要
ByteArrayOutputStream
。只要构造一个
FileOutputStream,
zipoutpstream
,以及一个
Writer

将字节数组转换为字符串没有性能差异,它也为您提供了存储的下降方式,我的意思是不会损坏。我需要编写二进制流。Zip只能通过程序读取,并且需要避免手动读取。这是否可能从二进制流中获取纯文本?当您写入二进制流时,它不是纯文本,而是您写入的数据是二进制或文本。在你的代码中,你认为你在写二进制代码吗?zos.write(baos.toByteArray());这是写二进制流。那么如何使内容二进制流呢?请稍等。在你问更多问题之前,先试试我的方法。