Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/367.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
打开使用java创建的zip文件时出错,该文件没有大小_Java_Android_Zipfile - Fatal编程技术网

打开使用java创建的zip文件时出错,该文件没有大小

打开使用java创建的zip文件时出错,该文件没有大小,java,android,zipfile,Java,Android,Zipfile,我用JAVA创建的zip文件有问题,我在这个问题上浪费了两天时间 我的Zip文件保存在设备的外部存储器上,但文件大小为0个八位字节,我无法打开此文件(错误提示:存档已损坏或格式未知(请参见下图) 这很奇怪,因为我可以毫无问题地在我的应用程序上读取和加载zip文件数据 这就是我创建zip文件的方式 File zipFile = new File(dir, activity.fileName + AppConstant.ZIP_EXTN); FilesUtils.refreshFil

我用JAVA创建的zip文件有问题,我在这个问题上浪费了两天时间

我的Zip文件保存在设备的外部存储器上,但文件大小为0个八位字节,我无法打开此文件(错误提示:存档已损坏或格式未知(请参见下图)

这很奇怪,因为我可以毫无问题地在我的应用程序上读取和加载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(relve r:listreve)
执行在第一个
finally
之后停止,然后转到最后一个finally,并发出与上面相同的警告。

由于zip文件为空,因此出现错误。问题一定是在zip文件创建部分。我建议您先尝试更简单的文件创建。并可能调用
objectOutputStream.close()
。好的,我将尝试简化文件创建。但是如果zip文件为空,为什么我可以读取应用程序中的内容并加载信息?