Question

SOLVED: ok so I am basically stupid. I couldn't open the file because I forgot to install winrar or 7zip since this pc is newly formatted... Everything works fine. Sorry to waste anyone's time.

In my app I programmatically generate a .zip file from photos and .csv files in a directory.

It creates the zip and then sends the email with the attachment without a hickup. The problem however is that on my pc I can't extract the .zip file because it says it's invalid, but on my device using "WinZip" I can check my .zip file and it has everything it is suppose to have. This is confusing me.

Here is my code: Here I check for which checkboxes have been checked then do the zipping

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)");
                }
            }

and here is the zip function:

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());
        }
    }
}
Was it helpful?

Solution

ok so I am basically stupid. I couldn't open the file because I forgot to install winrar or 7zip since this pc is newly formatted...

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top