0

I've coded a Class called DropboxHandler. This class manages all the stuff that interacts directly with my Dropbox account. This class has some methods like uploading and downloading a file, listing all files in a folder, and so on. Everything works fine, except with *.app Files. I know, that these are folders, but i cannot find out, how to download and save them on my HD. Here is my method to download an Folder/ File

public static void downloadFolder(String fileToDownload, String tempFileName) throws IOException {
    FileOutputStream outputStream = new FileOutputStream(tempFileName);
    try {
        DbxEntry.WithChildren listing = client.getMetadataWithChildren(fileToDownload);
        for (DbxEntry child : listing.children) {
            if (child instanceof DbxEntry.Folder) {
                (new File(tempFileName)).mkdirs();
                downloadFolder(fileToDownload + "/" + child.name, tempFileName + "/" + child.name);
            } else if (child instanceof DbxEntry.File) {
                DbxEntry.File downloadedFile = client.getFile(fileToDownload, null, outputStream);
                System.out.println("Metadata: " + downloadedFile.toString());
                System.out.println("Downloaded: " + downloadedFile.toString());
            }
        }

    } catch (DbxException e) {
        System.out.println(e.getMessage());
    } catch (IOException e) {
        System.out.println(e.getMessage());
    } finally {
        outputStream.close();
        System.out.println("Download finished");
    }
}

When i run my code, it creates a Folder called Launcher.app (Launcher is the File to download). But when it should download the content of the Launcher, the FileOutputStream fires an error that says that Launcher.app/Content isn't a Folder.

So maybe anyone has some Ideas, how to download the *.app "Files"

Greetings

DirtyNative
  • 2,553
  • 2
  • 33
  • 58
  • I don't think you've actually said what the problem is. You've pasted some code but haven't explained what's going wrong. Are you getting an error? What's the stack trace? Or is the app doing something other than what you expect? If so, what did you expect and what's the observed behavior? – user94559 Jan 29 '15 at 19:33
  • Sorry, i forgot it to mention ^^ – DirtyNative Jan 29 '15 at 21:00

1 Answers1

0

There are a number of issues with the code you posted. The one you're hitting now is that the very first line of the method creates a file with the same name as the folder you want to write to.

I think that the next issue you'll hit is where you call getFile. It looks like you're trying to save every file into the same output stream. So essentially you're creating a file (instead of a folder) called Launcher.app and then writing the contents of every file into that file (maybe concatenated together as one big file).

I took a stab at fixing the code, but I haven't tested it at all. (I don't even know if it compiles.) See if this helps:

// recursively download a folder from Dropbox to the local file system
public static void downloadFolder(String path, String destination) throws IOException {
    new File(destination).mkdirs();
    try {
        for (DbxEntry child : client.getMetadataWithChildren(path).children) {
            if (child instanceof DbxEntry.Folder) {
                // recurse
                downloadFolder(path + "/" + child.name, destination + "/" + child.name);
            } else if (child instanceof DbxEntry.File) {
                // download an individual file
                OutputStream outputStream = new FileOutputStream(
                    destination + "/" + child.name);
                try {
                    DbxEntry.File downloadedFile = client.getFile(
                        path + "/" + child.name, null, outputStream);
                } finally {
                    outputStream.close();
                }
            }
        }
    } catch (DbxException e) {
        System.out.println(e.getMessage());
    } catch (IOException e) {
        System.out.println(e.getMessage());
    }
}
user94559
  • 59,196
  • 6
  • 103
  • 103
  • Ahhh i see my problem. The output stream was still open when calling the method recursively. Well your version works, but now after everything was downloaded, i tried to run the app, but it says, that the Application couldn't be launched. What is wrong now? – DirtyNative Jan 30 '15 at 10:54
  • All I can do is guess, since I don't have access to the folder, but maybe file permissions? (Maybe the right files aren't marked as executable?) – user94559 Jan 30 '15 at 15:09