如何在android中将整个文件夹压缩为单个文件
我目前正在使用下面的代码来压缩文件夹,但不幸的是我无法压缩缓存文件夹,如果我尝试这样做,我只会创建一个伪压缩文件。。。 但它可以与位于内部和外部内存中的任何其他文件夹配合使用。。 我使用权限来写和读内部文件,但我仍然面临这个奇怪的问题 公共空拉链{如何在android中将整个文件夹压缩为单个文件,android,Android,我目前正在使用下面的代码来压缩文件夹,但不幸的是我无法压缩缓存文件夹,如果我尝试这样做,我只会创建一个伪压缩文件。。。 但它可以与位于内部和外部内存中的任何其他文件夹配合使用。。 我使用权限来写和读内部文件,但我仍然面临这个奇怪的问题 公共空拉链{ String p = "/data/data/com.example.****/cache/"; Date now = new Date(); File dir = new File (p,"up");
String p = "/data/data/com.example.****/cache/";
Date now = new Date();
File dir = new File (p,"up");
if (!dir.exists())
{
dir.mkdirs();
}
String fileName = formatter.format(now);
String _zipFile =p +"up/"+fileName+".zip";
try {
BufferedInputStream origin = null;
FileOutputStream dest = new FileOutputStream(_zipFile);
ZipOutputStream out = new ZipOutputStream(new BufferedOutputStream(dest));
File fileDir = new File("/data/data/com.example.****/cache/****/");
String[] _files = fileDir.list();
for ( int i = 0 ; i < _files.length ; i++ )
{
_files[i] = fileDir + "/"+ _files[i];
}
byte data[] = new byte[BUFFER];
for(int i=0; i < _files.length; i++) {
Log.v("Compress", "Adding: " + _files[i]);
FileInputStream fi = new FileInputStream(_files[i]);
origin = new BufferedInputStream(fi, BUFFER);
ZipEntry entry = new ZipEntry(_files[i].substring(_files[i].lastIndexOf("/") + 1));
out.putNextEntry(entry);
int count;
while ((count = origin.read(data, 0, BUFFER)) != -1) {
out.write(data, 0, count);
}
origin.close();
}
out.close();
} catch(Exception e) {
e.printStackTrace();
} }
您可以显示您的异常吗?不要硬编码路径。在某些上下文中使用getCacheDir,例如您的活动。我尝试了每种方法,但仍然没有成功:@MahdiHijazi java.io.FileNotFoundException:/data/data/com.example.**/**.png:open失败:EACCES权限被拒绝,是我断开了手机与电脑的连接并尝试了,但我没有安装我的内部存储器,我仍然面临着这个问题,我记得我使用了读写内部文件的权限