【发布时间】:2020-05-14 08:43:00
【问题描述】:
我需要将 100mb zip 文件(将包含子文件夹和图像)拆分为 10 个 zip 文件(每个 10mb)。然后我需要将每个切片的 zip 文件发送到 API(作为多部分 reauest) ,在接收器 API 中,我需要将上述 10 个 zip 文件中的每一个组合回原始 100mb zip 文件。
下面是切片的代码
public static void splitZip(String zipName, String location, String NewZip) throws IOException{
FileInputStream fis = new FileInputStream(location);
ZipInputStream zipInputStream = new ZipInputStream(fis);
ZipEntry entry = null;
int currentChunkIndex = 0;
long entrySize = 0;
ZipFile zipFile = new ZipFile(location);
Enumeration enumeration = zipFile.entries();
String copDest = zipCopyDest + "\\" + NewZip + "_" + currentChunkIndex +".zip";
FileOutputStream fos = new FileOutputStream(new File(copDest));
BufferedOutputStream bos = new BufferedOutputStream(fos);
ZipOutputStream zos = new ZipOutputStream(bos);
long currentSize = 0;
try {
while ((entry = zipInputStream.getNextEntry()) != null && enumeration.hasMoreElements()) {
ZipEntry zipEntry = (ZipEntry) enumeration.nextElement();
System.out.println(zipEntry.getName());
System.out.println(zipEntry.getSize());
entrySize = zipEntry.getSize();
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
if((currentSize + entrySize) > MAX_FILE_SIZE) {
zos.close();
currentChunkIndex++;
zos = getOutputStream(currentChunkIndex, NewZip);
currentSize = 0;
}else{
currentSize += entrySize;
zos.putNextEntry(new ZipEntry(entry.getName()));
byte[] buffer = new byte[8192];
int length = 0;
while ((length = zipInputStream.read(buffer)) > 0) {
outputStream.write(buffer, 0, length);
}
byte[] unzippedFile = outputStream.toByteArray();
zos.write(unzippedFile);
unzippedFile = null;
outputStream.close();
zos.closeEntry();
}
}
} finally {
zos.close();
}
}
当我手动提取切片拉链时,我发现一些图像已损坏无法打开。 也没有得到适当的解决方案来组合 zip 文件。提前致谢。
【问题讨论】:
-
是否要求每个单独的文件都必须是有效的 ZIP 文件?因为否则只是将字节流分成 10 MB 块在概念上和代码方面要容易得多。
-
一目了然,您似乎需要在两种情况下都执行
else部分(即是否切换到新的 ZIP 文件),否则您基本上会跳过每个发生的文件在 ZIP 文件边界处。 -
否 .. 只有在合并所有单独的 zip 文件后才有效。
-
在这种情况下,最简单的解决方案可能是忽略它是一个 ZIP 文件这一事实,只将 10MB 的块发送到服务器以接收,而根本不使用
Zip*类。 -
根据要求,我只需要使用 zip 文件..
标签: java spring-boot zip