空文件夹不会添加到压缩文件的android [英] Empty Folders are not adding to zip file android
问题描述
我在做一个Android项目。我需要压缩SD卡的完整文件夹结构。
code是能够压缩的文件夹,如果任何文件所在的文件夹内,否则跳过该文件夹。
I am doing an Android project. I need to zip a complete folder structure of SD card. code is able to zip the folder if any files exists inside the folder, else skips that folder.
我的文件夹结构如下:MNT / SD卡/电影/泰卢固/文件
My Folder structure is like: mnt/sdcard/Movies/Telugu/Files.
MNT / SD卡/电影/英语 - >英语是空文件夹
mnt/sdcard/Movies/English --> English is empty folder
我没有看到输出的zip文件夹英文
I did not see English folder in output zip file.
我的code:
public void zip() {
try {
// create a ZipOutputStream to zip the data to
ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(
_zipFile));
zipDir(folderPath, zos);
// close the stream
zos.close();
} catch (Exception e) {
// handle exception
}
}
public void zipDir(String dir2zip, ZipOutputStream zos) {
try {
File zipDir = new File(dir2zip);
// get a listing of the directory content
String[] dirList = zipDir.list();
byte[] readBuffer = new byte[2156];
int bytesIn = 0;
// loop through dirList, and zip the files
for (int i = 0; i < dirList.length; i++) {
File f = new File(zipDir, dirList[i]);
if (f.isDirectory()) {
// if the File object is a directory, call this
// function again to add its content recursively
String filePath = f.getPath();
zipDir(filePath, zos);
// loop again
continue;
}
// if we reached here, the File object f was not a directory
// create a FileInputStream on top of f
FileInputStream fis = new FileInputStream(f);
// create a new zip entry
ZipEntry anEntry = new ZipEntry(f.getPath());
// place the zip entry in the ZipOutputStream object
zos.putNextEntry(anEntry);
// now write the content of the file to the ZipOutputStream
while ((bytesIn = fis.read(readBuffer)) != -1) {
zos.write(readBuffer, 0, bytesIn);
}
// close the Stream
fis.close();
}
} catch (Exception e) {
e.printStackTrace();
// handle exception
}
}
请帮助..
推荐答案
您基本上都不创建的ZipEntry为您的目录。
You basically never create a zipentry for your directories.
在如果(f.isDirectory()){}
部分,补充一点:
zos.putNextEntry(new ZipEntry(filePath));
您也可以把它在方法的开头相反,包含的根目录,以及
You can also put it at the beginning of the method instead, to include the root dir as well.
这篇关于空文件夹不会添加到压缩文件的android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!