1

I have a folder with 3 picture inside of them which I wish to zip and email. I have a method that does this which I've used with previous problems and it works fine. However this time it keeps generating an invalid zip and when I open the zip it only has 1 picture inside with a size of 0. I can't seems to figure out why though. This is the method:

      //generate the zip file for the picture
      String zipFile = context.getExternalFilesDir(null) + "/ArcFlash/Checklist.zip";
      String srcDir = context.getExternalFilesDir(null) + "/ArcFlash/CheckListMedia";

      FileOutputStream fos = new FileOutputStream(zipFile);

      ZipOutputStream zos = new ZipOutputStream(fos);

      File srcFile = new File(srcDir);

      addDirToArchive(zos, srcFile, context);

here is my addDirToArchive method which generates the zip:

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

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

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

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

            String target = ctx.getExternalFilesDir(null) + "/";
            String oldPath = files[i].getPath();
            String newPath = oldPath.replace(target, "");

            zos.putNextEntry(new ZipEntry(newPath));
            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());
        }
    }
}

EDIT enter image description here

john
  • 3,949
  • 7
  • 34
  • 56

3 Answers3

2

Using the code samples below, here is how to do what you want:

final Path basePath = Paths.get(context.getExternalFilesDir(null));
final Path srcDir = Paths.resolve("ArcFlash/CheckListMedia");

final Path zipFile = Paths.resolve("ArcFlash/Checklist.zip");
final Map<String, Object> env = new HashMap<>();
env.put("create", "true");
final URI zip = URI.create("jar:file:" + zipFile.toAbsolutePath().toString());

try (
    final FileSystem fs = FileSystems.newFileSystem(zip, env, null);
) {
    Files.walkFileTree(srcDir, new CopyFileVisitor(srcDir, fs.getPath("/")));
}

First, a sample of how to create a zip file:

public final class ZipZip
{
    public static void main(final String... args)
        throws IOException
    {
        final Map<String, Object> env = new HashMap<>();
        env.put("create", "true");
        final URI zip = URI.create("jar:file:/tmp/t.zip");

        final Path sourceFile = Paths.get("/tmp/foo.txt");

        Files.deleteIfExists(Paths.get("/tmp/t.zip"));

        try (
            final FileSystem fs = FileSystems.newFileSystem(zip, env, null);
        ) {

            final Path zipdir = fs.getPath("/dir");
            Files.createDirectory(zipdir);

            final Path zipfile = zipdir.resolve("t.txt");
            Files.copy(sourceFile, zipfile);
        }
    }
}

Then, I have recently written a FileVisitor to recursively copy a directory, which is used here; here is its code:

public final class CopyFileVisitor
    implements FileVisitor<Path>
{
    private final Path srcdir;
    private final Path dstdir;

    public CopyFileVisitor(final Path srcdir, final Path dstdir)
    {
        this.srcdir = srcdir.toAbsolutePath();
        this.dstdir = dstdir.toAbsolutePath();
    }

    @Override
    public FileVisitResult preVisitDirectory(final Path dir,
        final BasicFileAttributes attrs)
        throws IOException
    {
        Files.createDirectories(toDestination(dir));
        return FileVisitResult.CONTINUE;
    }

    @Override
    public FileVisitResult visitFile(final Path file,
        final BasicFileAttributes attrs)
        throws IOException
    {
        System.out.printf("%s -> %s\n", file.toAbsolutePath(),
            toDestination(file));
        Files.copy(file, toDestination(file));
        return FileVisitResult.CONTINUE;
    }

    @Override
    public FileVisitResult visitFileFailed(Path file, IOException exc)
        throws IOException
    {
        throw exc;
    }

    @Override
    public FileVisitResult postVisitDirectory(Path dir, IOException exc)
        throws IOException
    {
        if (exc != null)
            throw exc;
        return FileVisitResult.CONTINUE;
    }

    private Path toDestination(final Path victim)
    {
        final Path tmp = victim.toAbsolutePath();
        final Path rel = srcdir.relativize(tmp);
        return dstdir.resolve(rel.toString());
    }
}
fge
  • 119,121
  • 33
  • 254
  • 329
  • Trying to implement this for my situation. Will report back – john Mar 20 '14 at 20:30
  • I've tried to implement the code however most of the methods like `FileVisitResult` are getting the `cannot resolve` error. Same with `Paths` – john Mar 21 '14 at 12:36
  • 1
    You mean a `ProviderMismatchException`? Yup, I have noticed this bug; I have since fixed the code, look at `toDestination()`. Before it was `dstdir.resolve(rel)`, it is now `dstdir.resolve(rel.toString())`. – fge Mar 21 '14 at 12:39
  • see the edit part, I posted a picture. I've included all the import necessary but it still doesn't recognize it – john Mar 21 '14 at 12:48
  • Are you sure you import `java.nio.file.FileSystem`? – fge Mar 21 '14 at 13:17
  • Also, this should be `java.nio.file.Path`. `Path` _does_ have a `.toAbsolutePath()` method but your capture says it is not recognized. – fge Mar 21 '14 at 13:19
  • android studio is funky. That's probably why it didn't give me the option to import it. This code definitely looks like it's very versatile but I think it might be too complicated for me. I've managed to figure out a solution to my problem just by tweaking the code I already have. I'll definitely be bookingmarking this page for future use. I'd like to thank you though for your troubles in trying to fix my stupidity lol – john Mar 21 '14 at 14:24
0

I strongly recommend you to use this library for zipping/unzipping contents:

http://www.lingala.net/zip4j/

Sebastian Breit
  • 6,137
  • 1
  • 35
  • 53
0

Be sure you are adding correct headers while making the file.

user3388324
  • 572
  • 5
  • 18