Copies a directory from a jar file to an external directory. : File « File « Android






Copies a directory from a jar file to an external directory.

    
/*
 * Copyright (C) 2010 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

//package com.google.doclava;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.Enumeration;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;

public class JarUtils {
  /**
   * Returns the jar file used to load class clazz, or defaultJar if clazz was not loaded from a
   * jar.
   */
  public static JarFile jarForClass(Class<?> clazz, JarFile defaultJar) {
    String path = "/" + clazz.getName().replace('.', '/') + ".class";
    URL jarUrl = clazz.getResource(path);
    if (jarUrl == null) {
      return defaultJar;
    }

    String url = jarUrl.toString();
    int bang = url.indexOf("!");
    String JAR_URI_PREFIX = "jar:file:";
    if (url.startsWith(JAR_URI_PREFIX) && bang != -1) {
      try {
        return new JarFile(url.substring(JAR_URI_PREFIX.length(), bang));
      } catch (IOException e) {
        throw new IllegalStateException("Error loading jar file.", e);
      }
    } else {
      return defaultJar;
    }
  }

  /**
   * Copies a directory from a jar file to an external directory.
   */
  public static void copyResourcesToDirectory(JarFile fromJar, String jarDir, String destDir)
      throws IOException {
    for (Enumeration<JarEntry> entries = fromJar.entries(); entries.hasMoreElements();) {
      JarEntry entry = entries.nextElement();
      if (entry.getName().startsWith(jarDir + "/") && !entry.isDirectory()) {
        File dest = new File(destDir + "/" + entry.getName().substring(jarDir.length() + 1));
        File parent = dest.getParentFile();
        if (parent != null) {
          parent.mkdirs();
        }

        FileOutputStream out = new FileOutputStream(dest);
        InputStream in = fromJar.getInputStream(entry);

        try {
          byte[] buffer = new byte[8 * 1024];

          int s = 0;
          while ((s = in.read(buffer)) > 0) {
            out.write(buffer, 0, s);
          }
        } catch (IOException e) {
          throw new IOException("Could not copy asset from jar file", e);
        } finally {
          try {
            in.close();
          } catch (IOException ignored) {}
          try {
            out.close();
          } catch (IOException ignored) {}
        }
      }
    }

  }

  private JarUtils() {} // non-instantiable
}

   
    
    
    
  








Related examples in the same category

1.Read the created file and display to the screen
2.Create a Uri for files on external storage
3.Create a Uri for files on internal storage
4.File read and write
5.View/Listen to media file
6.File Copy Util
7.Copy File Thread
8.Adaptation of the class File to add some usefull functions
9.Save to Android local file system
10.Using Ini file to manage Preferences
11.File Cache
12.Format File Size
13.File Upload
14.File Name Extension Utils
15.Write and append string to file
16.Transfers required files to the the private file system part in order to be access them from C Code.
17.returns a canonical line of a obj or mtl file.
18.Trims down obj files, so that they may be parsed faster later on.
19.Format File Size:KB, MB, GB
20.Get File Contents as String
21.Read File and return CharSequence
22.Copy File
23.Read/write From FileSystem, browse Account
24.Create Path and file under External Storage
25.Get File Extension Name
26.Extract File Name From String
27.Write String to file
28.Create an output file from raw resources.
29.Reads a file from /raw/res/ and returns it as a String
30.Read Config ini file
31.Read Write File Manager
32.Copy two files
33.Get the filename of the media file and use that as the default title
34.Copy File and Directory
35.Determines the MIME type for a given filename.
36.Move a file stored in the cache to the internal storage of the specified context
37.CharSequence from File
38.File Manager
39.Save Exception to a file
40.Delete a file and create directory
41.Get a list of filenames in this folder.
42.Delete Directory
43.Delete Directory 2
44.Delete Directory 3
45.Get readable folder size
46.Copy chars from a large (over 2GB) Reader to a Writer.