我有一个学校作业,要求我使用 apache commons compress library 接收输入流并将其压缩为具有 5 种格式之一(由用户规范)的字节数组。5 种格式是:ZIP、JAR、SEVENZ、BZIP2 和 GZIP。我编写了以下方法来压缩 JAR 格式的输入流,但我得到了一个带有字符串“No current entry”的非法状态异常。
private byte[] documentJARCompression(InputStream in) throws IOException {
BufferedInputStream buffIn = new BufferedInputStream(in);
ByteArrayOutputStream out = new ByteArrayOutputStream();
JarArchiveOutputStream jarOut = new JarArchiveOutputStream(out);
final byte[] buffer = new byte[out.size()];
int n = 0;
while (-1 != (n = buffIn.read(buffer))) {
jarOut.write(buffer, 0, n);
}
jarOut.close();
return buffer;
}