我正在使用 Java 的 ZipOutputStream 类来编写一个大的 zip 文件。
当 zip 文件只有 1000 个子文件和子文件夹时,它可以正常工作。当 zip 文件只有 10000 个子文件和子文件夹时,它也可以正常工作。
但是,由于某种原因,当我将其增加到超过 100000 个子文件和子文件夹时,就会出现问题。
它仍然会写入大量子文件,但随后会退出。我最终得到一个 zip 文件,其中包含我期望的大约一半的目录树。
zip 文件没有达到 4GB 的最大大小。完成后,zip 文件整体只有大约 30MB 大。zip 文件中的每个子文件的大小都小于 1KB。
zip 文件的布局很简单:
根文件夹应包含十个子目录。
这些子目录中的每一个都应包含十个子目录。
这一直持续到第四层,每个子目录包含十个文件。
但是执行后,第一级只保存了5个目录,而第一级的最后一个目录只保存了两个子目录。
以下是相关代码:
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();
}
}
}
和
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();
}
}
我想知道 ZipOutputStream 在退出之前是否有最大数量的文件可以写入。或者也许 FileOutputStream 可以。我不确定。
感谢您的帮助。谢谢你。