Java Copy Directory copyDirectoryFromJar(String jarName, String srcDir, File tmpDir)

Here you can find the source of copyDirectoryFromJar(String jarName, String srcDir, File tmpDir)

Description

copy Directory From Jar

License

Apache License

Declaration

public static void copyDirectoryFromJar(String jarName, String srcDir, File tmpDir) throws IOException 

Method Source Code


//package com.java2s;
/*//from  w  ww  .  ja  va2s.  co m
 * #%L
 * %%
 * Copyright (C) 2011 - 2016 BMW Car IT GmbH
 * %%
 * 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.
 * #L%
 */

import java.io.BufferedInputStream;

import java.io.File;
import java.io.FileInputStream;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import java.util.jar.JarEntry;
import java.util.jar.JarFile;
import java.util.jar.JarInputStream;

public class Main {
    public static void copyDirectoryFromJar(String jarName, String srcDir, File tmpDir) throws IOException {

        JarFile jf = null;
        JarInputStream jarInputStream = null;

        try {
            jf = new JarFile(jarName);
            JarEntry je = jf.getJarEntry(srcDir);
            if (je.isDirectory()) {
                FileInputStream fis = new FileInputStream(jarName);
                BufferedInputStream bis = new BufferedInputStream(fis);
                jarInputStream = new JarInputStream(bis);
                JarEntry ze = null;
                while ((ze = jarInputStream.getNextJarEntry()) != null) {
                    if (ze.isDirectory()) {
                        continue;
                    }
                    if (ze.getName().contains(je.getName())) {
                        InputStream is = jf.getInputStream(ze);
                        String name = ze.getName().substring(ze.getName().lastIndexOf("/") + 1);
                        File tmpFile = new File(tmpDir + "/" + name); //File.createTempFile(file.getName(), "tmp");
                        tmpFile.deleteOnExit();
                        OutputStream outputStreamRuntime = new FileOutputStream(tmpFile);
                        copyStream(is, outputStreamRuntime);
                    }
                }
            }
        } finally {
            if (jf != null) {
                jf.close();
            }
            if (jarInputStream != null) {
                jarInputStream.close();
            }
        }
    }

    public static void copyStream(InputStream in, OutputStream out) throws IOException {
        int len;
        byte[] buffer = new byte[64 * 1024];
        while ((len = in.read(buffer)) > 0) {
            out.write(buffer, 0, len);
        }

        in.close();
        out.close();
    }
}

Related

  1. copyDirectoryContent(File srcPath, File targetPath)
  2. copyDirectoryContents(File directory, String targetDirName, boolean update)
  3. copyDirectoryContents(File source, File destination)
  4. copyDirectoryContents(File src, File target)
  5. copyDirectoryContents(File srcDir, File dstDir)
  6. copyDirectoryTree(File copyFrom, File copyTo)
  7. copyDirectoryWithContents(final File srcPath, final File dstPath)