john
john

Reputation: 4158

Programmatically generating zip creates invalid zip[android]

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

Upvotes: 2

Views: 828

Answers (3)

fge
fge

Reputation: 121710

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

Upvotes: 2

user3388324
user3388324

Reputation: 572

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

Upvotes: 0

Sebastian Breit
Sebastian Breit

Reputation: 6159

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

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

Upvotes: 0

Related Questions