java解压出来损坏_ZipOutputStream创建损坏的(可解压缩的)zip文件

我正在尝试将压缩字节发送到另一台服务器,然后让该服务器接收它们并写出压缩文件。当我在同一台服务器上进行压缩和编写时,它的效果非常好。本地版本看起来像这样:

ZipOutputStream zout = new ZipOutputStream(FileOutputStream);

zout.write(byteBuffer, 0, len);

zout.flush()

FileOutputStream.flush();

zout.close();

但跨服务器实现会创建一个错误的输出。发送代码是:(魔术字符串告诉服务器它已收到所有数据。

ZipOutputStream zout = new ZipOutputStream(out);

ZipEntry entry = new ZipEntry(fileName);

zout.putNextEntry(entry);

System.out.println("sending zipped bytes...");

zout.write(inputBuffer, contentBegin, len);

zout.flush();

zout.closeEntry();

out.flush();

byte[] magicStringData = "--------MagicStringCSE283Miami".getBytes("US-ASCII");

out.write(magicStringData, 0, magicStringData.length);

out.flush();

System.out.println("Done writing file and sending zipped bytes.");

Thread.sleep(10000);

zout.close();

clntSock.close(); // Close the socket. We are done with this client!

接收代码如下所示:

System.out.println("receiving zipped bytes...");

byte[] inputBuffer = new byte[BUF_SIZE];

int total2 = 0, count = 0;

while(count != -1) { // read from origin's buffer into byteBuffer until origin is out of data

count = inFromCompression.read(inputBuffer, total2, BUF_SIZE - total - 1);

String msg = new String(inputBuffer, total2, count, "US-ASCII");

total2 += count;

if(msg.contains("-------MagicString")){

System.out.println("full message received...");

break;

}

}

String inputString = new String(inputBuffer, 0, total2, "US-ASCII");

int contentEnd = inputString.indexOf("--------MagicString");

FileOutputStream fout2 = new FileOutputStream(outputFileName + ".zip");

fout2.write(inputBuffer, 0, contentEnd);

fout2.flush();

fout2.close();

System.out.println("Done writing zipped bytes.");

//Thread.sleep(10000);

//socketToCompression.close();

有什么想法吗?我想这可能是因为我没有在发送表示数据结束的魔术字符串之前关闭ZipOutputStream,但每次在刷新zout后立即调用zout.close()它会关闭整个套接字。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值