1

我有一个文件夹,里面有 3 张图片,我希望将其压缩并通过电子邮件发送。我有一种方法可以做到这一点,我已经在以前的问题中使用过它,并且效果很好。然而,这一次它不断生成一个无效的 zip,当我打开 zip 时,它里面只有 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);

这是我addDirToArchive生成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());
        }
    }
}

编辑 在此处输入图像描述

4

3 回答 3

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

然后,我最近写了一个FileVisitor递归复制一个目录,这里用到;这是它的代码:

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());
    }
}
于 2014-03-20T20:27:51.787 回答
0

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

于 2014-03-20T20:29:40.657 回答
0

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

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

于 2014-03-20T20:19:13.437 回答