程序生成的zip在PC上无效,但在android上无效

时间:2014-01-31 14:22:31

标签: android zip zipoutputstream

解决: 好的,所以我基本上是傻瓜。我无法打开文件,因为我忘了安装winrar或7zip,因为这台电脑是新格式化的...一切正常。很抱歉浪费任何人的时间。

在我的应用程序中,我以编程方式从目录中的照片和.csv文件生成.zip文件。

它创建了zip,然后发送带有附件的电子邮件而没有hickup。然而问题是,在我的电脑上,我无法提取.zip文件,因为它说它无效,但在我的设备上使用“WinZip”我可以检查我的.zip文件,它有它应有的一切。这让我很困惑。

这是我的代码: 在这里,我检查已经检查了哪些复选框然后执行压缩

for(int i = 0; i < cbStates.size(); ++i)
            {
                if(cbStates.get(i))
                {
                    String zipFile = Environment.getExternalStorageDirectory() + "/ArcFlash/" + listItems.get(i) + ".zip";//ex: /storage/sdcard0/ArcFlash/study12.zip
                    String srcDir = Environment.getExternalStorageDirectory() + "/ArcFlash/" + listItems.get(i);

                    try
                    {
                        FileOutputStream fos = new FileOutputStream(zipFile);

                        ZipOutputStream zos = new ZipOutputStream(fos);

                        File srcFile = new File(srcDir);

                        Log.i("customException", "going to compress");
                        addDirToArchive(zos, srcFile);

                        // close the ZipOutputStream
                        zos.close();
                    }

                    catch (IOException ioe)
                    {
                        System.out.println("Error creating zip file: " + ioe);
                    }

                    //Send the email
                    Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
                    emailIntent.setType("application/image");
                    emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, new String[]{"jbasson@powercoreeng.com"});
                    emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT,"Test Subject");
                    emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, "From My App");

                    String folderPath = Environment.getExternalStorageDirectory() + "/ArcFlash/" + listItems.get(i) + ".zip";
                    //Uri u = Uri.fromFile(folderPath);

                    //Log.i("customException", "uri path: " + u.getPath());
                    emailIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse("file://" + folderPath));
                    startActivity(Intent.createChooser(emailIntent, "Send mail..."));

                    Toast.makeText(context,"Case \"" + studyName + "\" has been sent", Toast.LENGTH_LONG).show();
                    //adapter.setElement(i, adapter.getStudy(i) + "(sent)");
                }
            }

这是zip函数:

private static void addDirToArchive(ZipOutputStream zos, File srcFile)
{
    File[] files = srcFile.listFiles();

    Log.i("customException", "Adding directory: " + srcFile.getName());

    for (int i = 0; i < files.length; i++)
    {
        // if the file is directory, use recursion
        if (files[i].isDirectory())
        {
            addDirToArchive(zos, files[i]);
            continue;
        }
        try
        {
            System.out.println("tAdding file: " + files[i].getName());

            // create byte buffer
            byte[] buffer = new byte[2048];//1024

            FileInputStream fis = new FileInputStream(files[i]);

            zos.putNextEntry(new ZipEntry(files[i].getAbsolutePath() + "/" + files[i].getName()));//files[i].getName()
            int length;
            while ((length = fis.read(buffer)) > 0)
            {
                zos.write(buffer, 0, length);
            }

            zos.closeEntry();

            // close the InputStream
            fis.close();
        }
        catch (Exception ex)
        {
            Log.i("customException", "error zipping: " + ex.getMessage());
        }
    }
}

1 个答案:

答案 0 :(得分:0)

好吧所以我基本上是傻瓜。我无法打开文件,因为我忘了安装winrar或7zip,因为这台电脑是新格式化的......