从多个zip文件中读取数据并将其组合为一个 [英] Reading data from multiple zip files and combining them to one
问题描述
我想从比如说zip1,zip2,zip3,zip4的4个zip文件中读取数据.所有这些zip文件都是从称为"BigZip"的1个大zip文件中分离出来的.我想将zip文件合并为一个,然后比较1个bigzip文件与(zip1 + zip2 + zip3 + zip4)合并后的zip文件的字节大小是否匹配.当我合并4个zip文件的大小时,我得到的文件大小非常小.我在做什么错了?
I want to read data from lets say 4 zip files called zip1, zip2, zip3, zip4. All of these zip files are split from this 1 big zip file called "BigZip". I want to combine the zip files into one and then compare the bytes if the 1 bigzip file matches the size of bytes with the combined zip file of (zip1+zip2+zip3+zip4). I am getting a very small file size when I combine the size of 4 zip files. What am I doing wrong?
这是我的相同代码:
targetFilePath1,targetFilePath2,targetFilePath3,targetFilePath4属于4个zip文件的路径. sourceFilePath是BigZip文件的路径
targetFilePath1, targetFilePath2, targetFilePath3, targetFilePath4 belongs to path of 4 zip files. sourceFilePath is the path to BigZip file
class Test {
public static void main(String args[]) {
ZipOutputStream outStream = new ZipOutputStream(new FileOutputStream(sourceBigZip));
readZip(sourceFilePath, targetFilePath1);
readZip(sourceFilePath, targetFilePath2);
readZip(sourceFilePath, targetFilePath3);
readZip(sourceFilePath, targetFilePath4);
outStream.close();
}
static void readZip(String sourceBigZip, String targetFile) throws Exception {
ZipInputStream inStream = new ZipInputStream(new FileInputStream(targetFile));
byte[] buffer = new byte[1024];
int len = inStream.read(buffer);
while (len != -1) {
outStream.write(buffer, 0, len);
len = inStream.read(buffer);
System.out.print(len);
}
inStream.close();
}
}
推荐答案
创建一次ZipOutputStream
并将其传递给readZip()
方法,例如:
Create ZipOutputStream
once and pass it to readZip()
method, like:
public static void main(String args[]) {
ZipOutputStream outStream = new ZipOutputStream(new FileOutputStream(sourceFilePath));
readZip(outStream , targetFilePath1);
readZip(outStream , targetFilePath2);
readZip(outStream , targetFilePath3);
readZip(outStream , targetFilePath4);
}
然后在处理将数据从一个zip复制到另一个zip时出现错误... 您需要像这样复制zip文件中的每个文件:
Then you have an error dealing with copying the data from one zip to another... You need to copy each file in the zip file like this:
static void readZip(ZipOutputStream outStream, String targetFile)
throws Exception {
ZipInputStream inStream = new ZipInputStream(new FileInputStream(
targetFile));
byte[] buffer = new byte[1024];
int len = 0;
for (ZipEntry e; (e = inStream.getNextEntry()) != null;) {
outStream.putNextEntry(e);
while ((len = inStream.read(buffer)) > 0) {
outStream.write(buffer, 0, len);
}
}
inStream.close();
}
}
这篇关于从多个zip文件中读取数据并将其组合为一个的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!