views:

155

answers:

2

Hey Java - Developers

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

+1  A: 

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.

Yishai
A: 

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