Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/308.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:ZipoutStream将数据块压缩到单个zip文件_Java_Arrays_Compression_Zipoutputstream - Fatal编程技术网

Java:ZipoutStream将数据块压缩到单个zip文件

Java:ZipoutStream将数据块压缩到单个zip文件,java,arrays,compression,zipoutputstream,Java,Arrays,Compression,Zipoutputstream,问题的后续行动: 我可以在没有中间文件(或内存文件)的情况下压缩数据。我现在需要压缩数据块并将它们添加到单个压缩文件中 我使用的是前一个问题中建议的单个ZipoutStream String infile = "test.txt"; FileInputStream in = new FileInputStream(infile); String outfile = "test.txt.zip"; FileOutputStream out = new FileOutputStream(outfi

问题的后续行动:

我可以在没有中间文件(或内存文件)的情况下压缩数据。我现在需要压缩数据块并将它们添加到单个压缩文件中

我使用的是前一个问题中建议的单个ZipoutStream

String infile = "test.txt";
FileInputStream in = new FileInputStream(infile);

String outfile = "test.txt.zip";
FileOutputStream out = new FileOutputStream(outfile);

ByteArrayOutputStream baos = new ByteArrayOutputStream();
ZipOutputStream zos = new ZipOutputStream(baos);
ZipEntry entry = new ZipEntry("test_unzip.txt");
entry.setSize(2048);
zos.putNextEntry(entry);

int len = 0;
while (len > -1) {
      byte[] buf = new byte[10];
      len = in.read(buf);
      zos.write(buf);
      out.write(baos.toByteArray());
      baos.reset();
}

zos.closeEntry();
zos.finish();
zos.close();

in.close();
out.close();
我尝试了不同大小的
buf
、重新排序
zos.finish
zos.closeEntry
,也尝试了使用和不使用
baos.reset

我还试着将infle的全部内容读入一个buf中,但仍然不起作用

我希望有一个有效的.zip文件可以解压缩到test_unzip.txt中。 但是,当我尝试在命令行上解压test.txt.zip时,会出现以下错误:

End-of-central-directory signature not found.  Either this file is not
  a zipfile, or it constitutes one disk of a multi-part archive.  In the latter case the central directory and zipfile comment will be found on the last disk(s) of this archive.
unzip:  cannot find zipfile directory in one of test.txt.zip or
        test.txt.zip.zip, and cannot find test.txt.zip.ZIP, period.

“你看过这样的例子吗?”路易斯·沃瑟曼再次感谢你的快速回复。是的,我试过一个例子,它可以
ZipOutputStream(FileOutputStream)
,而且效果非常好!但是我需要使用
ZipOutputStream(ByteArrayOutputStream)
。这是因为如前一个问题所述,我需要在内部使用ZipOutputStream实现
byte[]compress(byte[])
。您看过类似的示例吗?@Louis Wasserman再次感谢您的快速回复。是的,我试过一个例子,它可以
ZipOutputStream(FileOutputStream)
,而且效果非常好!但是我需要使用
ZipOutputStream(ByteArrayOutputStream)
。这是因为如前一个问题所述,我需要在内部使用ZipOutputStream实现
byte[]压缩(byte[])