Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

This is super useful. Any chance for a recursive option to copy directories? #3

Closed
mikeymckay opened this Issue · 7 comments

3 participants

@mikeymckay

No description provided.

@gkcgautam
Owner

Good idea. Will add it very soon.

@cagnulein

News about recursion?
Great work man!

@cagnulein

Copy recursive dir, very dirty, but it works

String startActivity(String assetFile, String destinationDirLocation, String destinationFile) throws IOException {
    File sd_path = Environment.getExternalStorageDirectory();   // Path to the SD Card in the device

    if(destinationDirLocation.indexOf(sd_path.toString())!=0) {
        destinationDirLocation = sd_path+"/"+destinationDirLocation;
    }
    File destDirectory = new File(destinationDirLocation);

    if (destDirectory.exists() && !destDirectory.isDirectory())
        throw new IOException("Can't create directory, a file is in the way");
    if (!destDirectory.exists()) {
        // Create destination directory if it doesn't already exist
        destDirectory.mkdirs();
        if (!destDirectory.isDirectory()) {
            throw new IOException("Unable to create directory");
        }
    }

    String finalFileName = assetFile;
    if(destinationFile != null && destinationFile.length()>0) {
        finalFileName = destinationFile;
    }

    AssetManager assets = this.cordova.getActivity().getApplicationContext().getAssets();
    String[] list = assets.list(assetFile);
    for(int i=0; i<list.length; i++) {  
        if(list[i].contains(".")) {
            InputStream in = this.cordova.getActivity().getApplicationContext().getAssets().open(addTrailingSlash(assetFile) + list[i]);
            String d = addTrailingSlash(destinationDirLocation) + list[i];
            OutputStream out = new FileOutputStream(d);

            // Transfer bytes from in to out
            byte[] buf = new byte[1024];
            int len; while ((len = in.read(buf)) > 0) out.write(buf, 0, len);
            in.close(); out.close();
        }
        else {
            startActivity(addTrailingSlash(assetFile) + list[i], addTrailingSlash(destinationDirLocation) + list[i], "" );
        }
    }
    return "";
}
@gkcgautam
Owner

@cagnulein Will improve your code and merge into the plugin whenever i get time.
Can you help me with testing?

@cagnulein

No problem :-)

@gkcgautam
Owner

@mikeymckay @cagnulein I have updated the plugin and added support for copying directories.
I have also changed the plugin usage method to make it easier.

Could you please give it a try and report any issues you come across?

@gkcgautam gkcgautam closed this
@gkcgautam
Owner

To be honest, I haven't tested the code! :smile_cat:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.