3

I have application store and applications have their url. I want to download apks from those urls to my jaggery server. Although below code(my first solution) create myApp.apk successfully, its not work properly.

First i tried to below code,

var url  = "http://img.xxx.com/006/someApp.apk";
var data = get(url, {});    

var file = new File("myApp.apk");
    file.open("w");
    file.write(data.data);
    file.close(); 


when i print data.data value, its look like

data.data



i also tried,
var file = new File("http://img.xxx.com/006/someApp.apk");
file.saveAs("myApp.txt");

Can anyone help me?

Community
  • 1
  • 1
kemalsami
  • 337
  • 5
  • 17
  • Hmm... A data stream that begins with "PK". Have you tried to unzip it? – tjd May 06 '15 at 14:24
  • No. I didn't try to unzip it. I am sorry but I dont understand why i should unzip it?? – kemalsami May 06 '15 at 14:33
  • @user3306495 - because PK is the signature of a .zip file. Since they're binary (contain non-printable characters) a better test of success than trying to print the contents would be to try to open the result with a zip program. – enhzflep May 06 '15 at 14:35
  • 1
    PK -> Phil Katz is one of the co-creators of the .zip format. His initials show up at the beginning of all such files. One should also be aware that one of the oldest files for manipulating zip files is PKZip, by PKWare – tjd May 06 '15 at 14:40
  • Unzip solution didnt work for me.. First I saved file as .apk and .zip then I tried to unzip both however i got central directory not found error.. Is there any solution other than unzip file?? By the way, thanks for PK info :) – kemalsami May 06 '15 at 14:53

2 Answers2

0

.apk files are Android application files, and they are expected to start with PK, because they are actually zip archives!

They're not meant to be unzipped, although you can do it to see some of the application resources (but there are better ways for reverse engineering .apk files such as Apktool, if that's what you're looking for).

minipif
  • 4,756
  • 3
  • 30
  • 39
  • In fact, question is about downloading something with given url. It can be jpg, json, xml or apk no matter type. – kemalsami May 08 '15 at 07:34
  • As you wrote `below code create myApp.apk successfully`, it was understood that the file was successfully created, but its content not what you expected, especially since you replied to the comments about unzipping its content. If you question is purely about downloading data from a URL and saving it to a file, you might want to re-phrase your question, without focusing on the file extension and the actual data. – minipif May 08 '15 at 07:59
0

According to jaggery documentations, file.write is writing the String representation of the object to the file. So that's why you are getting an apk file which cannot be installed.

However you can make it work using copyURLToFile in apache commons-io java library as follows since jaggery supports java itself and all of WSO2 products have apache commons-io library in their class path.

<%
    var JFileUtils = Packages.org.apache.commons.io.FileUtils;
    var JUrl = Packages.java.net.URL;
    var JFile = Packages.java.io.File;
    var url  = new JUrl("http://img.xxx.com/006/someApp.apk");
    JFileUtils.copyURLToFile(url, new JFile("myApp.apk"));
    print("done");
%>

Your file will be stored on $CARBON_HOME directory by default, unless you specified relative or absolute path to the file.

Charitha
  • 470
  • 3
  • 10