【问题标题】:android create zip file with txt filesandroid创建带有txt文件的zip文件
【发布时间】:2014-03-10 03:42:12
【问题描述】:

我想用 txt 文件创建一个 zip 文件。每个 txt 文件都是一个字符串。这是我的代码

        File f = new File("test.zip");
        f.mkdirs(); 
        try {
            ZipOutputStream out = new ZipOutputStream(new FileOutputStream(f));
            out.putNextEntry(new ZipEntry("testfolder/mytext.txt"));

            byte[] data = stringInTXT.toString().getBytes();
            out.write(data, 0, data.length);
            out.closeEntry();
            out.close();
        } catch (IOException e) {
            System.out.println(e.toString());
        }

我得到一个 FileNotFoundException

ZipOutputStream out = new ZipOutputStream(new FileOutputStream(f));

这是用 txt 文件创建 zip 文件的正确方法吗?如果是这样,我将如何解决此异常?

【问题讨论】:

  • 请确认文件存在!!通过在 if 块中检查目录/文件是否存在,然后以这样的方式进行,如果目录/文件不存在而不是创建一个新目录/文件,请始终使用完整路径作为资源之类的路径!

标签: android zip


【解决方案1】:

您必须输入整个路径,包括驱动器号和文件扩展名, at new File("test.zip");

【讨论】:

  • 安卓有默认目录吗?我只需要在将 zip 文件上传到服务器之前临时创建它。
  • 有两种方式。sdcard路径,/data/data/你的包名/你的临时文件..
猜你喜欢
  • 1970-01-01
  • 2017-01-22
  • 2023-03-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-04-28
  • 2017-07-19
相关资源
最近更新 更多