android使用txt文件创建zip文件

android使用txt文件创建zip文件,android,zip,Android,Zip,我想用txt文件创建一个zip文件。每个txt文件都是一个字符串。这是我的密码 File f = new File("test.zip"); f.mkdirs(); try { ZipOutputStream out = new ZipOutputStream(new FileOutputStream(f)); out.putNextEntry(new ZipEntry("testfolder/my

我想用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());
        }
我在上收到一个FileNotFound异常

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

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

您必须键入整个路径,包括驱动器号和文件扩展名,
在新文件中(“test.zip”)

请确保该文件存在!!通过检查if块中是否存在目录/文件,然后以这样一种方式进行操作:如果目录/文件不存在,则创建一个新目录/文件,始终为类似路径的资源使用完整路径!!android有默认目录吗?我只需要在上传到服务器之前临时创建zip文件。有两种方法。sdcard路径,/data/data/your package name/your temporary files。。