Android 没有将空文件夹添加到zip文件中
我正在做一个Android项目。我需要压缩一个完整的SD卡文件夹结构。 如果文件夹中存在任何文件,则代码可以压缩该文件夹,否则跳过该文件夹 我的文件夹结构类似于:mnt/sdcard/Movies/Telugu/Files mnt/sdcard/Movies/English-->英语为空文件夹 我在输出zip文件中没有看到英文文件夹 我的代码:Android 没有将空文件夹添加到zip文件中,android,zip,zipoutputstream,Android,Zip,Zipoutputstream,我正在做一个Android项目。我需要压缩一个完整的SD卡文件夹结构。 如果文件夹中存在任何文件,则代码可以压缩该文件夹,否则跳过该文件夹 我的文件夹结构类似于:mnt/sdcard/Movies/Telugu/Files mnt/sdcard/Movies/English-->英语为空文件夹 我在输出zip文件中没有看到英文文件夹 我的代码: public void zip() { try { // create a ZipOutputStream to zip the
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
}
}
public void zip(){
试一试{
//创建ZipOutStream以将数据压缩到
ZipOutputStream zos=新的zipoutpstream(新文件输出流(
_zipFile);
zipDir(folderPath,zos);
//关闭小溪
zos.close();
}捕获(例外e){
//处理异常
}
}
public void zipDir(字符串dir2zip、ZipOutputStream zos){
试一试{
File zipDir=新文件(dir2zip);
//获取目录内容的列表
字符串[]dirList=zipDir.list();
字节[]读取缓冲区=新字节[2156];
int字节数=0;
//循环浏览目录列表,并压缩文件
对于(int i=0;i
请帮助..您基本上从未为您的目录创建zipentry 在
if(f.isDirectory()){}
部分中,添加以下内容:
zos.putNextEntry(new ZipEntry(filePath));
您也可以将它放在方法的开头,以包括根目录