Java Jar Zip File addFileToJar(File baseDir, File src, JarOutputStream zOut)

Here you can find the source of addFileToJar(File baseDir, File src, JarOutputStream zOut)

Description

add File To Jar

License

Open Source License

Declaration

private static void addFileToJar(File baseDir, File src, JarOutputStream zOut) throws IOException 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2010-2015 BSI Business Systems Integration AG.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:/*from  w  ww  .  j  a  v a 2s . c  om*/
 *     BSI Business Systems Integration AG - initial API and implementation
 ******************************************************************************/

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

import java.io.IOException;

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

public class Main {
    private static void addFileToJar(File baseDir, File src, JarOutputStream zOut) throws IOException {
        String name = src.getAbsolutePath();
        String prefix = baseDir.getAbsolutePath();
        if (prefix.endsWith("/") || prefix.endsWith("\\")) {
            prefix = prefix.substring(0, prefix.length() - 1);
        }
        name = name.substring(prefix.length() + 1);
        name = name.replace('\\', '/');
        long timestamp = src.lastModified();
        byte[] data = readFile(src);
        addFileToJar(name, data, timestamp, zOut);
    }

    private static void addFileToJar(String name, byte[] data, long timestamp, JarOutputStream zOut)
            throws IOException {
        ZipEntry entry = new ZipEntry(name);
        entry.setTime(timestamp);
        zOut.putNextEntry(entry);
        zOut.write(data);
        zOut.closeEntry();
    }

    private static byte[] readFile(File source) throws IOException {
        if (!source.exists()) {
            throw new FileNotFoundException(source.getAbsolutePath());
        }
        if (!source.canRead()) {
            throw new IOException("cannot read " + source);
        }
        if (source.isDirectory()) {
            // source can not be a directory
            throw new IOException("source is a directory: " + source);
        }
        try (FileInputStream input = new FileInputStream(source)) {
            byte[] data = new byte[(int) source.length()];
            int n = 0;
            while (n < data.length) {
                n += input.read(data, n, data.length - n);
            }
            return data;
        }
    }
}

Related

  1. addFileToJar(File file, ZipOutputStream zipStream, String path)
  2. addFileToJar(File fileOrDirectoryToAdd, File extractedJarPath, JarOutputStream target)
  3. addFileToJar(JarOutputStream jar, InputStream file, String path)
  4. addJar(File path)