Example usage for java.util.jar JarOutputStream JarOutputStream

List of usage examples for java.util.jar JarOutputStream JarOutputStream

Introduction

In this page you can find the example usage for java.util.jar JarOutputStream JarOutputStream.

Prototype

public JarOutputStream(OutputStream out, Manifest man) throws IOException 

Source Link

Document

Creates a new JarOutputStream with the specified Manifest.

Usage

From source file:com.zimbra.cs.zimlet.ZimletUtil.java

public static void createZip(String dirName, String descFile) throws IOException {
    File dir = new File(dirName);
    if (!dir.exists() || !dir.isDirectory()) {
        throw new IOException("directory does not exist: " + dirName);
    }//from  w ww .  ja v  a  2  s  .c  om
    String target = descFile;
    boolean found = false;
    for (String f : dir.list()) {
        if (target != null) {
            if (target.compareTo(f) == 0) {
                found = true;
                break;
            }
        } else if (f.endsWith(".xml") && f.substring(0, f.length() - 4).compareTo(dir.getName()) == 0) {
            target = f;
            found = true;
            break;
        }
    }
    if (!found) {
        throw new IOException("Zimlet description not found, or not named correctly.");
    }
    String manifest = "Manifest-Version: 1.0\nZimlet-Description-File: " + target + "\n";
    JarOutputStream out = new JarOutputStream(
            new FileOutputStream(target.substring(0, target.length() - 4) + ".zip"),
            new Manifest(new ByteArrayInputStream(manifest.getBytes("UTF-8"))));
    for (File f : dir.listFiles()) {
        addZipEntry(out, f, null);
    }
    out.close();
}

From source file:org.apache.phoenix.end2end.UserDefinedFunctionsIT.java

/**
 * Compiles the test class with bogus code into a .class file.
 * Upon finish, the bogus jar will be left at dynamic.jar.dir location
 *///from   w  w w. j  ava2  s. com
private static void compileTestClass(String className, String program, int counter) throws Exception {
    String javaFileName = className + ".java";
    File javaFile = new File(javaFileName);
    String classFileName = className + ".class";
    File classFile = new File(classFileName);
    String jarName = "myjar" + counter + ".jar";
    String jarPath = "." + File.separator + jarName;
    File jarFile = new File(jarPath);
    try {
        String packageName = "org.apache.phoenix.end2end";
        FileOutputStream fos = new FileOutputStream(javaFileName);
        fos.write(program.getBytes());
        fos.close();

        JavaCompiler jc = ToolProvider.getSystemJavaCompiler();
        int result = jc.run(null, null, null, javaFileName);
        assertEquals(0, result);

        Manifest manifest = new Manifest();
        manifest.getMainAttributes().put(Attributes.Name.MANIFEST_VERSION, "1.0");
        FileOutputStream jarFos = new FileOutputStream(jarPath);
        JarOutputStream jarOutputStream = new JarOutputStream(jarFos, manifest);
        String pathToAdd = packageName.replace('.', '/') + '/';
        String jarPathStr = new String(pathToAdd);
        Set<String> pathsInJar = new HashSet<String>();

        while (pathsInJar.add(jarPathStr)) {
            int ix = jarPathStr.lastIndexOf('/', jarPathStr.length() - 2);
            if (ix < 0) {
                break;
            }
            jarPathStr = jarPathStr.substring(0, ix);
        }
        for (String pathInJar : pathsInJar) {
            jarOutputStream.putNextEntry(new JarEntry(pathInJar));
            jarOutputStream.closeEntry();
        }

        jarOutputStream.putNextEntry(new JarEntry(pathToAdd + classFile.getName()));
        byte[] allBytes = new byte[(int) classFile.length()];
        FileInputStream fis = new FileInputStream(classFile);
        fis.read(allBytes);
        fis.close();
        jarOutputStream.write(allBytes);
        jarOutputStream.closeEntry();
        jarOutputStream.close();
        jarFos.close();

        assertTrue(jarFile.exists());
        Connection conn = driver.connect(url, EMPTY_PROPS);
        Statement stmt = conn.createStatement();
        stmt.execute("add jars '" + jarFile.getAbsolutePath() + "'");
    } finally {
        if (javaFile != null)
            javaFile.delete();
        if (classFile != null)
            classFile.delete();
        if (jarFile != null)
            jarFile.delete();
    }
}

From source file:org.olat.core.util.i18n.I18nManager.java

/**
 * Create a jar file that contains the translations for the given languages.
 * <p>//from  ww  w.j a va  2s .  c  o  m
 * Note that this file is created in a temporary place in olatdata/tmp. It is
 * in the responsibility of the caller of this method to remove the file when
 * not needed anymore.
 * 
 * @param languageKeys
 * @param fileName the name of the file.
 * @return The file handle to the created file or NULL if no such file could
 *         be created (e.g. there already exists a file with this file name)
 */
public File createLanguageJarFile(Collection<String> languageKeys, String fileName) {
    // Create file olatdata temporary directory
    File file = new File(WebappHelper.getTmpDir() + "/" + fileName);
    file.getParentFile().mkdirs();

    FileOutputStream stream = null;
    JarOutputStream out = null;
    try {
        // Open stream for jar file
        stream = new FileOutputStream(file);
        out = new JarOutputStream(stream, new Manifest());
        // Use now as last modified date of resources
        long now = System.currentTimeMillis();
        // Add all languages
        for (String langKey : languageKeys) {
            Locale locale = getLocaleOrNull(langKey);
            // Add all bundles in the current language
            for (String bundleName : I18nModule.getBundleNamesContainingI18nFiles()) {
                Properties propertyFile = getPropertiesWithoutResolvingRecursively(locale, bundleName);
                String entryFileName = bundleName.replace(".", "/") + "/" + I18N_DIRNAME + "/"
                        + buildI18nFilename(locale);
                // Create jar entry for this path, name and last modified
                JarEntry jarEntry = new JarEntry(entryFileName);
                jarEntry.setTime(now);
                // Write properties to jar file
                out.putNextEntry(jarEntry);
                propertyFile.store(out, null);
                if (isLogDebugEnabled()) {
                    logDebug("Adding file::" + entryFileName + " + to jar", null);
                }
            }
        }
        logDebug("Finished writing jar file::" + file.getAbsolutePath(), null);
    } catch (Exception e) {
        logError("Could not write jar file", e);
        return null;
    } finally {
        try {
            out.close();
            stream.close();
        } catch (IOException e) {
            logError("Could not close stream of jar file", e);
            return null;
        }
    }
    return file;
}