简体   繁体   English

如何在zip中写入字节数组,然后从那里读取

[英]How to write an array of bytes in zip and then read it from there

Sorry for my English. 对不起我的英语不好。 I need zzat array of bytes (I do it through zip), but I do not use files, channels and buffers. 我需要扎兹字节数组(我通过zip来完成),但是我不使用文件,通道和缓冲区。 After that I need to unload (unzip this array to another array) I did something like this but it doesn't work: 之后,我需要卸载(将该数组解压缩到另一个数组),我做了类似的事情,但是它不起作用:

public class Main {
    public static void main(String[] args) {
        byte[] b = "Help me please".getBytes();               
        ByteArrayOutputStream baos = new ByteArrayOutputStream();     
        try {
            baos.write(b);
        } catch (IOException e) {
            e.printStackTrace();
        }
        try (ZipOutputStream zos = new ZipOutputStream(baos)){
            ZipEntry out = new ZipEntry("1");
            zos.putNextEntry(out);

            zos.closeEntry();
        }
        catch (IOException e){
            e.printStackTrace();
        }
        byte[] a = baos.toByteArray();                               //compressed array

        ByteArrayInputStream bais = new ByteArrayInputStream(a);
        try(ZipInputStream zis = new ZipInputStream(bais)){
                System.out.println('1');
                byte[]c = zis.readAllBytes();
                zis.closeEntry();
                System.out.println(c.equals(b));
        }
        catch (IOException e){
            e.printStackTrace();
        }
    }
}

The following worked for me. 以下对我有用。 Note that I open the Zip file stream first, then I open the entry, then I write the bytes. 请注意,我首先打开Zip文件流,然后打开条目,然后写入字节。 It has to go in that order or it doesn't work. 它必须按此顺序执行,否则将无法正常工作。

public class ZipFileTest {

   public static void main( String[] args ) throws IOException {
      byte[] b = "Help me please".getBytes( "UTF-8" );
      ByteArrayOutputStream baos = new ByteArrayOutputStream();
      try( ZipOutputStream zos = new ZipOutputStream( baos ) ) {
         ZipEntry out = new ZipEntry( "1 First" );
         zos.putNextEntry( out );
         zos.write( b, 0, b.length );
         zos.closeEntry();
      }

      byte[] a = baos.toByteArray();        //compressed array
      ByteArrayInputStream bais = new ByteArrayInputStream( a );

      try( ZipInputStream zis = new ZipInputStream( bais ) ) {
         for( ZipEntry zipe; (zipe = zis.getNextEntry()) != null; ) {
            byte[] data = new byte[1024];
            int length = zis.read( data, 0, data.length );
            System.out.println( "Entry: " + zipe.toString() );
            System.out.println( "Data: " + new String( data, 0, length, "UTF-8" ) );
            zis.closeEntry();
         }
      }
   }
}

Output: 输出:

run:
Entry: 1 First
Data: Help me please
BUILD SUCCESSFUL (total time: 0 seconds)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从文件中读取特定数量的字节到Java中的bytes数组中? - How to read a specific number of bytes from a file into a bytes array in Java? 从MQSeries读/写字符字节 - Read/write character bytes from MQSeries 如何从字节数组制作文件并从这些文件制作 zip 并将 zip 发送到服务器而不在 Android 的存储中创建文件? - How to make files from an array of bytes and make zip from these files and send zip to the server without creating files in the storage of Android? 如何使用datainput流从文件中读取字节数组,并在Java中打印字节数组 - How to read an array of bytes from a file using datainput stream, and print the array of bytes in java 如何在Java中读写zip文件? - How to read and write a zip file in java? 拆分先前从文件读取的字节数组 - Split an array of Bytes previously read from a file 如何从非常重的文件中读取字节? 然后将其存储在字符串中,例如.pdf .zip .xlsx文件 - How can i read bytes from a very heavy file? then store store them in a String e.g. .pdf .zip .xlsx files 如何在Java中将字节数组写入二进制文件 - How to write array of bytes to a binary file in Java 如何在BluetoothChat中从输入/输出流读取/写入原始十六进制字节? - How do I read/write raw hex bytes from input/output stream in BluetoothChat? 如何存储字节并将其读回数组 - How to store bytes and read them back to an array
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM