Java ZipoutStream写入一些文件,但不写入其他文件
我正在使用Java的ZipoutStream类来编写一个大的zip文件 当zip文件只有1000个子文件和子文件夹时,它可以正常工作。 当zip文件只有10000个子文件和子文件夹时,它也可以正常工作 但是,由于某种原因,当我将其提升到100000多个子文件和子文件夹时,问题就开始出现了 它仍将写入大量子文件,但随后将退出。最后我得到了一个zip文件,其中包含了我所期望的目录树的一半 zip文件的大小没有达到4GB。完成后,zip文件的总大小只有大约30MB。zip文件中的每个子文件的大小都小于1KB zip文件的布局很简单: 根文件夹应包含十个子目录 这些子目录中的每一个子目录都应该包含十个子目录 这一直持续到第四级,每个子目录包含十个文件 但在执行后,第一级仅包含5个目录,第一级的最后一个目录仅包含两个子目录 以下是相关代码:Java ZipoutStream写入一些文件,但不写入其他文件,java,zip,fileoutputstream,zipoutputstream,bufferedoutputstream,Java,Zip,Fileoutputstream,Zipoutputstream,Bufferedoutputstream,我正在使用Java的ZipoutStream类来编写一个大的zip文件 当zip文件只有1000个子文件和子文件夹时,它可以正常工作。 当zip文件只有10000个子文件和子文件夹时,它也可以正常工作 但是,由于某种原因,当我将其提升到100000多个子文件和子文件夹时,问题就开始出现了 它仍将写入大量子文件,但随后将退出。最后我得到了一个zip文件,其中包含了我所期望的目录树的一半 zip文件的大小没有达到4GB。完成后,zip文件的总大小只有大约30MB。zip文件中的每个子文件的大小都小于
public class MyZipFile{
MyFolder folder;
public void create(String[] haystack){
folder = new MyFolder();
folder.create(haystack);
}
public void writeToDisk(String rootPath){
FileOutputStream rootFile;
BufferedOutputStream rootBuffer;
ZipOutputStream rootZip;
try{
rootFile = new FileOutputStream(rootPath);
rootBuffer = new BufferedOutputStream(rootFile);
rootZip = new ZipOutputStream(rootBuffer);
folder.writeToDisk(rootZip);
rootZip.close();
}
}
}
及
我想知道ZipOutputStream在退出之前是否可以写入最大数量的文件。或者FileOutputStream会。我不确定
谢谢你的帮助。谢谢。Java实现的原始INFO-ZIP格式似乎仅限于65536个条目: 这里是WinZIP相对于ZIP64格式的另一个限制角度:
您可能需要使用不同的存档格式或找到限制文件数量的方法。@PickenChoose您是否达到了我在这里描述的INFO-ZIP限制?如果是,请接受并投票:谢谢。这就解释了一切。我试图对你的评论投赞成票,但它说我需要赢得15的声誉才能公开展示我的投票。很抱歉
public class MyKeyFolder extends MyFileSystemElement {
private final int numSubfiles = 10;
private MyFileSystemElement[] subfiles;
public MyFolder(int[] catalogNumber){
super(catalogNumber);
subfiles = new MyFileSystemElement[numSubfiles];
}
@Override
public void create(String[] haystack){
for(int i=0; i < numSubfiles; i++){
MyFileSystemElement element;
int[] subCatalogNumber = createSubCatalogNumber(i);
//finalFolderLevel determines how many files are in the ZIP
//When finalFolderLevel is 2, there are about 1000 subfiles.
//When finalFolderLevel is 3, there are about 10000 subfiles.
//When finalFolderLevel is 4, there are about 100000 subfiles
//(and this is where I run into trouble).
if(thisFolderLevel <= finalFolderLevel)
element = new myFolder(subCatalogNumber);
else
element = new myFile(subCatalogNumber);
subfiles[i] = element;
subfiles[i].create(haystack);
}
}
@Override
public void writeToDisk(ZipOutputStream zipStream) throws IOException {
String path = createPathFromCatalogNumber();
zipStream.putNextEntry(new ZipEntry(path));
zipStream.flush();
zipStream.closeEntry();
for(MyFileSystemElement file : subfiles){
file.writeToDisk(zipStream);
}
}
}
public class MyFile extends MyFileSystemElement {
private String fileContents;
@Override
public void create(String[] haystack){
fileContents = "";
fileContents += haystack[deriveIndex(0)] + "\n";
fileContents += haystack[deriveIndex(1)] + "\n";
fileContents += haystack[deriveIndex(2)] + "\n";
fileContents += haystack[deriveIndex(3)] + "\n";
fileContents += haystack[deriveIndex(4)] + "\n";
fileContents += haystack[deriveIndex(5)] + "\n";
fileContents += haystack[deriveIndex(6)] + "\n";
fileContents += haystack[deriveIndex(7)] + "\n";
fileContents += haystack[deriveIndex(8)] + "\n";
fileContents += haystack[deriveIndex(9)] + "\n";
}
@Override
public void writeToDisk(ZipOutputStream zipStream) throws IOException {
String path = createPathFromCatalogNumber();
ZipEntry entry = new ZipEntry(path);
byte[] contents = fileContents.getBytes();
zipStream.putNextEntry(entry);
zipStream.write(contents);
zipStream.flush();
zipStream.closeEntry();
}
}