Java BufferedReader Copy copyFileFromJar(File jarFile, File targetFile, String sourceFilePath)

Here you can find the source of copyFileFromJar(File jarFile, File targetFile, String sourceFilePath)

Description

copy File From Jar

License

LGPL

Declaration

public static void copyFileFromJar(File jarFile, File targetFile, String sourceFilePath) 

Method Source Code


//package com.java2s;
/*/*from  w  w  w.  j  av  a  2 s .c  o m*/
 * @author     kebin, ucchy
 * @license    LGPLv3
 * @copyright  Copyright kebin, ucchy 2013
 */

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;

import java.util.jar.JarFile;
import java.util.zip.ZipEntry;

public class Main {

    public static void copyFileFromJar(File jarFile, File targetFile, String sourceFilePath) {

        InputStream is = null;
        FileOutputStream fos = null;
        BufferedReader reader = null;
        BufferedWriter writer = null;

        File parent = targetFile.getParentFile();
        if (!parent.exists()) {
            parent.mkdirs();
        }

        try {
            JarFile jar = new JarFile(jarFile);
            ZipEntry zipEntry = jar.getEntry(sourceFilePath);
            is = jar.getInputStream(zipEntry);

            fos = new FileOutputStream(targetFile);

            reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
            writer = new BufferedWriter(new OutputStreamWriter(fos));

            String line;
            while ((line = reader.readLine()) != null) {
                writer.write(line);
                writer.newLine();
            }

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (writer != null) {
                try {
                    writer.flush();
                    writer.close();
                } catch (IOException e) {
                    // do nothing.
                }
            }
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e) {
                    // do nothing.
                }
            }
            if (fos != null) {
                try {
                    fos.flush();
                    fos.close();
                } catch (IOException e) {
                    // do nothing.
                }
            }
            if (is != null) {
                try {
                    is.close();
                } catch (IOException e) {
                    // do nothing.
                }
            }
        }
    }
}

Related

  1. copyFile(String src, String dst)
  2. copyFile(String srcFile, String dstFile)
  3. copyFile(String srcFileName, String destFileName)
  4. copyFile2(String source, String out)
  5. copyFileContentsIntoAnotherFile(String inputFileName, PrintWriter Out)
  6. copyFileLinewise(File inputFile, File outputFile)
  7. copyFileRecursivlyLinewise(File inDir, File outDir)
  8. copyFileTextMode(String source, String destination)
  9. copyFileWithAppend(File source, File destination)