我的JAVA创建的zip文件有问题,我在这个问题上失败了两天。
我的Zip文件保存在我的设备的外部存储器中,但文件的大小是0个八位字节,我无法打开此文件(错误说:存档损坏或未知格式(见下图)。< / p>
这很奇怪,因为我可以毫无问题地在我的应用上阅读和加载zip文件数据。
这就是我创建我的zip文件的方式
File zipFile = new File(dir, activity.fileName + AppConstant.ZIP_EXTN);
FilesUtils.refreshFile(activity, zipFile);
ZipOutputStream zos = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(zipFile),102400));
try
{
zos.putNextEntry(new ZipEntry(AppConstant.SESSION_FILENAME));
try
{
ObjectOutputStream objectOutputStream = new ObjectOutputStream(zos);
objectOutputStream.writeObject(session);
objectOutputStream.flush();
} finally
{
zos.closeEntry();
}
for (Releve r : listReleve)
{
for (String photoPath : r.getListPathPhoto())
{
File file = new File(photoPath);
if(file.exists()){
FileInputStream fis = new FileInputStream(file);
ZipEntry zipEntry = new ZipEntry(AppConstant.PATH_TO_PHOTO_ZIP_FOLDER+file.getName());
zos.putNextEntry(zipEntry);
FilesUtils.copyStream(fis,zos);
fis.close();
}
}
}
} finally{
zos.close();
}
这就是我阅读zip文件的方式。
private Session readFromZipExternalStorage(File sessionZip) throws IOException
{
Session sessionToReturn = null;
ObjectInputStream in = null;
ZipInputStream zis = new ZipInputStream(new FileInputStream(sessionZip));
try
{
ZipEntry ze = zis.getNextEntry();
while(ze!=null)
{
if(ze.getName().endsWith(AppConstant.SESSION_EXTN))
{
in = new ObjectInputStream(zis);
try {
sessionToReturn = (Session) in.readObject();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
ze = zis.getNextEntry();
}
}finally {
in.close();
zis.close();
}
return sessionToReturn;
}
在这里你有logcat警告。
07-09 14:58:29.182: W/System.err(9735): at java.util.zip.ZipInputStream.checkClosed(ZipInputStream.java:368)
07-09 14:58:29.182: W/System.err(9735): at java.util.zip.ZipInputStream.closeEntry(ZipInputStream.java:125)
07-09 14:58:29.182: W/System.err(9735): at java.util.zip.ZipInputStream.getNextEntry(ZipInputStream.java:212)
07-09 14:58:29.182: W/System.err(9735): at com.app.rir.activity.gestionsession.HelperbuildTab.readFromZipExternalStorage(HelperConstructionTableau.java:93)
07-09 14:58:29.182: W/System.err(9735): at com.app.rir.activity.gestionsession.HelperBuildTab.buildTabBody(HelperConstructionTableau.java:49)
07-09 14:58:29.182: W/System.err(9735): at com.app.rir.activity.gestionsession.ManageSessionActivity.onResume(ManageSessionActivity.java:80)
07-09 14:58:24.925: W/System.err(9735): java.io.IOException: Stream is closed
07-09 14:58:24.936: W/System.err(9735): at java.util.zip.ZipOutputStream.checkOpen(ZipOutputStream.java:427)
07-09 14:58:24.940: W/System.err(9735): at java.util.zip.ZipOutputStream.closeEntry(ZipOutputStream.java:132)
07-09 14:58:24.940: W/System.err(9735): at com.app.rir.activity.principale.HelperSauvegardeSession.saveSession(HelperSauvegardeSession.java:98)
07-09 14:58:24.940: W/System.err(9735): at com.app.rir.activity.principale.HelperSauvegardeSession.doInBackground(HelperSauvegardeSession.java:50)
07-09 14:58:24.940: W/System.err(9735): at com.app.rir.activity.principale.HelperSauvegardeSession.doInBackground(HelperSauvegardeSession.java:1)
07-09 14:58:24.940: W/System.err(9735): at android.os.AsyncTask$2.call(AsyncTask.java:292)
07-09 14:58:24.940: W/System.err(9735): at java.util.concurrent.FutureTask.run(FutureTask.java:237)
07-09 14:58:24.940: W/System.err(9735): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
07-09 14:58:24.940: W/System.err(9735): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
07-09 14:58:24.940: W/System.err(9735): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
07-09 14:58:24.941: W/System.err(9735): at java.lang.Thread.run(Thread.java:818)
修改
正如dotvav所说,我尝试objectOutputStream.close()
,在刷新之后,现在文件有一个大小,我可以在Windows上打开zip,但图像不会添加到zip文件中。
我是debbug,我发现,我没有在第一个for (Releve r : listReleve)
之后传递双循环finally
执行停止,并且像上面一样发出最后一个警告。