Java Jar Zip File addToJar(File source, JarOutputStream jarOutput)

Here you can find the source of addToJar(File source, JarOutputStream jarOutput)

Description

add To Jar

License

Apache License

Declaration

private static void addToJar(File source, JarOutputStream jarOutput) throws IOException 

Method Source Code


//package com.java2s;
/*/*w ww.ja  v a 2s. c  o m*/
 * Copyright to the original author or authors.
 *
 * 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.
 */

import java.io.*;

import java.util.jar.JarEntry;
import java.util.jar.JarOutputStream;

public class Main {
    private static void addToJar(File source, JarOutputStream jarOutput) throws IOException {
        BufferedInputStream in = null;
        try {
            if (source.isDirectory()) {
                String name = source.getPath().replace("\\", "/");
                if (!name.isEmpty()) {
                    if (!name.endsWith("/"))
                        name += "/";
                    JarEntry entry = new JarEntry(name);
                    entry.setTime(source.lastModified());
                    jarOutput.putNextEntry(entry);
                    jarOutput.closeEntry();
                }
                for (File nestedFile : source.listFiles())
                    addToJar(nestedFile, jarOutput);
                return;
            }
            String name = source.getPath().replace("\\", "/").substring("target/test-classes/".length());
            JarEntry entry = new JarEntry(name);
            entry.setTime(source.lastModified());
            jarOutput.putNextEntry(entry);
            in = new BufferedInputStream(new FileInputStream(source));

            byte[] buffer = new byte[1024];
            while (true) {
                int count = in.read(buffer);
                if (count == -1)
                    break;
                jarOutput.write(buffer, 0, count);
            }
            jarOutput.closeEntry();
        } finally {
            if (in != null)
                in.close();
        }
    }
}

Related

  1. addJarLibralySystemClassPath(File jarPath)
  2. addJARs(File dir)
  3. addJars(File directory, boolean recursive)
  4. addJarsToClassPath(String jarPath)
  5. addJarToClasspath(File jarFile)
  6. addToJar(JarOutputStream target, String pathInsideJar, File fentry)