Java Jar Zip Folder addFolderToJar(File baseDir, File srcdir, JarOutputStream zOut)

Here you can find the source of addFolderToJar(File baseDir, File srcdir, JarOutputStream zOut)

Description

add Folder To Jar

License

Open Source License

Declaration

private static void addFolderToJar(File baseDir, File srcdir, 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  .  ja  v a  2  s.c o  m
 *     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 addFolderToJar(File baseDir, File srcdir, JarOutputStream zOut) throws IOException {
        if ((!srcdir.exists()) || (!srcdir.isDirectory())) {
            throw new IOException("source directory " + srcdir + " does not exist or is not a folder");
        }
        for (File f : srcdir.listFiles()) {
            if (f.exists() && (!f.isHidden())) {
                if (f.isDirectory()) {
                    addFolderToJar(baseDir, f, zOut);
                } else {
                    addFileToJar(baseDir, f, zOut);
                }
            }
        }
    }

    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;
        }
    }
}