我想压缩一个文件夹。实际上我管理它。但是有一个问题。如果我的目录中有一个空文件夹,则不会在我的zip文件中创建空文件夹 例如我的目录是:
/ folder
/folder/temp.txt
/ folder / emptyfolder
当我用我的代码压缩此目录时,我的zip文件中不存在emptyfolder。我发现problem.Problem是files.length == 0所以for循环不起作用。但我没有找到解决方案。如何在我的zip文件中创建空文件夹?
private static final void zip(File directory, File base,
ZipOutputStream zos) throws IOException {
File[] files;
if(directory.isDirectory()) // folder
{
files = directory.listFiles();
}else
{
files = new File[1]; // file
files[0] = directory;
}
byte[] buffer = new byte[8192];
int read = 0;
for (int i = 0, n = files.length; i < n; i++)
{
if (files[i].isDirectory())
{
zip(files[i], base, zos);
}
else
{
FileInputStream in = new FileInputStream(files[i]);
ZipEntry entry = new ZipEntry(files[i].getPath().substring(
base.getPath().lastIndexOf("/") + 1));
zos.putNextEntry(entry);
while (-1 != (read = in.read(buffer)))
{
zos.write(buffer, 0, read);
}
in.close();
}
}