我想壓縮多個目錄。這意味着我有1個內部目錄和1個父目錄。現在我想壓縮父目錄。如何壓縮文件包括android中的子文件夾
我使用如下代碼:
我的文件路徑:
/data/data/com/app/1430159400000/32640/Images/capture_Image_20150427_115541.png"
/data/data/com/app/1430159400000/32640/Images/capture_Image_20150427_115542.png"
/data/data/com/app/1430159400000/32640/Images/ChildImages/capture_Image_20150427_115543.png"
/data/data/com/app/1430159400000/32640/Images/ChildImages/capture_Image_20150427_115544.png"
/data/data/com/app/1430159400000/32640/Images/capture_Image_20150427_115545.png"
/data/data/com/app/1430159400000/32640/Images/capture_Image_20150427_115546.png"
從目錄中獲取文件: -
public void getListFilesForCreatingZip(File parentDir) {
String[] filesPath = null;
File[] files = parentDir.listFiles();
filesPath = new String[(int) parentDir.length()];
int index = 0;
int index1=0;
for (File file : files) {
if (file.isDirectory()) {
if(file.getName().equals("ChildImages"))
{
File[] files1 = file.listFiles();
for(File ss:files1)
{
filesPath[index]=ss.getPath();
index++;
}
}
} else {
filesPath[index] = file.getPath();
}
index++;
}
zip(filesPath, "PathName";
}
zip文件: -
public void zip(String[] _files, String zipFileName) {
try {
BufferedInputStream origin = null;
/*File root = Application.getInstance().getDir("bol", Context.MODE_PRIVATE);
File customDir = new File(root + File.separator + File.separator + PreferenceManagerForBol.getInstance().getBolSelectedDate() + File.separator + PreferenceManagerForBol.getInstance().getBolOrderNumber());
if (customDir.exists() == false) {
customDir.mkdirs();
} */
File file = new File(BolDetailsHandler.getInstance().createBasePath(), zipFileName + ".zip");
FileOutputStream dest = new FileOutputStream(file);
ZipOutputStream out = new ZipOutputStream(new BufferedOutputStream(
dest));
byte data[] = new byte[BUFFER];
for (int i = 0; i < _files.length; i++) {
if(!_files[i].contains(".zip"))
{
Logger.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();
}
}
當我使用以下代碼我在zip()方法中面臨空指針異常。當zip()方法嘗試讀取子路徑並再次嘗試讀取父路徑時,它顯示空指針異常。
請讓我有任何想法來解決這個問題。
不要嘗試調用空對象的方法 – 1615903