以编程方式生成zip创建无效的zip [android]

时间:2014-03-20 20:14:51

标签: java android fileinputstream zipoutputstream

我有一个包含3张图片的文件夹,我希望压缩和发送电子邮件。我有一个方法可以做到这一点,我已经用过以前的问题,它工作正常。但是这次它一直生成无效的拉链,当我打开拉链时,里面只有1张图片,大小为0。 我似乎无法弄明白为什么。这是方法:

      //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);

这是我生成zip的addDirToArchive方法:

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

修改 enter image description here

3 个答案:

答案 0 :(得分:2)

使用下面的代码示例,以下是您想要的方法:

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

首先,如何创建zip文件的示例:

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

然后,我最近写了a FileVisitor to recursively copy a directory,在这里使用;这是它的代码:

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

答案 1 :(得分:0)

我强烈建议您使用此库来压缩/解压缩内容:

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

答案 2 :(得分:0)

确保在制作文件时添加正确的标题。