Java - 复制 Jar 文件夹

发布于 2024-08-29 14:34:43 字数 378 浏览 12 评论 0原文

实际上我遇到了一个问题。我的应用程序的一个包中有一个“.apk 文件”。 apk 是一种 jar 文件(apk = Android Package)。 我现在想将此 jar 文件从我的程序复制到 PC 上的任何其他位置。 通常我会使用以下方法来做到这一点:

FileInputStream is = new FileInputStream(this.getClass().getResource("/resources/myApp.apk").getFile());

然后使用 FileOutputStream 将其写入磁盘上。 ...但是由于 .apk 是一种 .jar,因此它不起作用。它只是复制 .apk 文件。但不包含其他文件。

任何帮助将不胜感激

Actually I am confronted with a Problem. I've got a ".apk-File" in one Package of my Application. apk is a kind of a jar File (apk = Android Package).
I now want to copy this jar-file out of my Programm onto any other Location at the PC.
Normally I would do this by using:

FileInputStream is = new FileInputStream(this.getClass().getResource("/resources/myApp.apk").getFile());

And then write it on the disk with using a FileOutputStream.
... but since an .apk is a kind of a .jar it doesn't work. It just copies the .apk file. but without the containing other files.

any help would be appreciated

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

人疚 2024-09-05 14:34:43

由于 .apk 是另一个名称的 .jar 文件(换句话说,它是一个 zip 文件,其中包含配置文件在目录中存储位置的一些特定定义),然后查看在 ZipInputStream 阅读文件并浏览内容并将它们写为文件。

Since .apk is a .jar file by another name (in other words it is a zip file with some specific definitions of where configuration files are stored inside the directory) then look at ZipInputStream to read the file and walk through the contents and write them out as files.

锦上情书 2024-09-05 14:34:43

非常感谢 Yishai...这就是我一直在等待的提示:)
可能有人需要做同样的事情,因此......这是我的代码:

public static boolean copyApkFile(File outputFile){
        try {
            FileInputStream fis = new FileInputStream(this.getClass().getResource("/resources/myApkFile.apk").getFile());
            ZipInputStream zis = new ZipInputStream(fis);
            FileOutputStream fos = new FileOutputStream(outputFile));
            ZipOutputStream zos = new ZipOutputStream(fos);
            ZipEntry ze = null;
            byte[] buf = new byte[1024];
            while ((ze = zis.getNextEntry()) != null) {
                System.out.println("Next entry "+ze.getName()+" "+ze.getSize());
                zos.putNextEntry(ze);
                int len;
                while ((len = zis.read(buf)) > 0) {
                  zos.write(buf, 0, len);
                }
            }
            zos.close();
            fos.close();
            zis.close();
            fis.close();
            return true;
        } catch (IOException ex) {
            Logger.getLogger(SetUpNewDevice.class.getName()).log(Level.SEVERE, null, ex);
            return false;
        }
    }

Thank you very much Yishai... this was the Hint I was waiting for :)
Probably is sb out there, who needs the to do a same thing, therefore... here is my code:

public static boolean copyApkFile(File outputFile){
        try {
            FileInputStream fis = new FileInputStream(this.getClass().getResource("/resources/myApkFile.apk").getFile());
            ZipInputStream zis = new ZipInputStream(fis);
            FileOutputStream fos = new FileOutputStream(outputFile));
            ZipOutputStream zos = new ZipOutputStream(fos);
            ZipEntry ze = null;
            byte[] buf = new byte[1024];
            while ((ze = zis.getNextEntry()) != null) {
                System.out.println("Next entry "+ze.getName()+" "+ze.getSize());
                zos.putNextEntry(ze);
                int len;
                while ((len = zis.read(buf)) > 0) {
                  zos.write(buf, 0, len);
                }
            }
            zos.close();
            fos.close();
            zis.close();
            fis.close();
            return true;
        } catch (IOException ex) {
            Logger.getLogger(SetUpNewDevice.class.getName()).log(Level.SEVERE, null, ex);
            return false;
        }
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文