1

I have a .zip file in my assets folder, which I want to copy 1-1 to a .zip in a folder on my device.

It kind of works, but the output zip contains also stuff that I do not understand.

What I have:

Input:

assets: Map.mp3

Output:

Map.zip

assets+META-INF+org+res+AndroidManifest.xml+classes.dex+resources.arsc (all APK file)

Whereas in Map.zip/assets there is a Map.mp3 with the initial content. But I just need to have a 1-1 copy with just file extension changed.

My code:

//taken from: http://stackoverflow.com/questions/4447477/android-how-to-copy-files-from-assets-folder-to-sdcard
        AssetManager assetManager = getAssets();
        AssetFileDescriptor assetFileDescriptor = null;

        try
        {
            assetFileDescriptor = assetManager.openFd("Map.mp3");
            // Create new file to copy into.
            File output= new File(_FILEPATH_ + java.io.File.separator + "Map.zip");
            output.createNewFile();
            copyFdToFile(assetFileDescriptor.getFileDescriptor(), output);
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }


public static void copyFdToFile(FileDescriptor src, File dst) throws IOException {
    FileChannel inChannel = new FileInputStream(src).getChannel();
    FileChannel outChannel = new FileOutputStream(dst).getChannel();
    try {
        inChannel.transferTo(0, inChannel.size(), outChannel);
    } finally {
        if (inChannel != null)
            inChannel.close();
        if (outChannel != null)
            outChannel.close();
    }
}

_FILEPATH_=Environment.getExternalStorageDirectory().getAbsolutePath()+"/osmdroid"

Alexey Zimarev
  • 17,944
  • 2
  • 55
  • 83
A.L.
  • 144
  • 2
  • 9

1 Answers1

0

You can always use simple file copy procedure:

private void copyFile(InputStream in, OutputStream out) throws IOException {
    byte[] buffer = new byte[1024];
    int read;
    while((read = in.read(buffer)) != -1){
      out.write(buffer, 0, read);
    }
}

Method with usage of FileDescriptor is not working with compressed files. As I understand, you get such solution from that answer. The comment below,

My solution was to change the extension to .mp3, then remove it once copied

that suggest to rename extension to .mp3 is dated by Dec 19 '12 at 2:07. Google may change FileDescriptor usage and close that thrick with renaming.

I'm using simple procedure in my Android project and all works fine, there is no performance issues.

Community
  • 1
  • 1
Artem Mostyaev
  • 3,874
  • 10
  • 53
  • 60