Example usage for java.util.jar JarOutputStream closeEntry

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

Introduction

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

Prototype

public void closeEntry() throws IOException 

Source Link

Document

Closes the current ZIP entry and positions the stream for writing the next entry.

Usage

From source file:com.textocat.textokit.postagger.opennlp.PackageModelZipAsArtifact.java

public static void main(String[] args) throws IOException {
    PackageModelZipAsArtifact cli = new PackageModelZipAsArtifact();
    new JCommander(cli, args);
    Path inputZipPath = Paths.get(cli.inputZipPathStr);
    if (!Files.isRegularFile(inputZipPath)) {
        System.err.println(inputZipPath + " is not an existing file.");
        System.exit(1);/* www. j a va  2  s .  com*/
    }
    POSModelJarManifestBean manifestBean = new POSModelJarManifestBean(cli.languageCode, cli.modelVariant);
    Path outputJarPath = inputZipPath
            .resolveSibling(FilenameUtils.getBaseName(inputZipPath.getFileName().toString()) + ".jar");
    try (OutputStream out = new BufferedOutputStream(Files.newOutputStream(outputJarPath))) {
        JarOutputStream jout = new JarOutputStream(out, manifestBean.toManifest());
        jout.putNextEntry(new ZipEntry(ClasspathPOSModelHolder.getClassPath(manifestBean.getLanguageCode(),
                manifestBean.getModelVariant())));
        FileUtils.copyFile(inputZipPath.toFile(), jout);
        jout.closeEntry();
        jout.close();
    }
}

From source file:com.textocat.textokit.morph.opencorpora.resource.XmlDictionaryPSP.java

public static void main(String[] args) throws Exception {
    XmlDictionaryPSP cfg = new XmlDictionaryPSP();
    new JCommander(cfg, args);

    MorphDictionaryImpl dict = new MorphDictionaryImpl();
    DictionaryExtension ext = cfg.dictExtensionClass.newInstance();
    FileInputStream fis = FileUtils.openInputStream(cfg.dictXmlFile);
    try {/*w  w  w .ja  v a 2  s.  c  om*/
        new XmlDictionaryParser(dict, ext, fis).run();
    } finally {
        IOUtils.closeQuietly(fis);
    }

    System.out.println("Preparing to serialization...");
    long timeBefore = currentTimeMillis();
    OutputStream fout = new BufferedOutputStream(FileUtils.openOutputStream(cfg.outputJarFile), 8192 * 8);
    Manifest manifest = new Manifest();
    manifest.getMainAttributes().putValue(Attributes.Name.MANIFEST_VERSION.toString(), "1.0");
    manifest.getMainAttributes().putValue(OpencorporaMorphDictionaryAPI.ME_OPENCORPORA_DICTIONARY_VERSION,
            dict.getVersion());
    manifest.getMainAttributes().putValue(OpencorporaMorphDictionaryAPI.ME_OPENCORPORA_DICTIONARY_REVISION,
            dict.getRevision());
    manifest.getMainAttributes().putValue(OpencorporaMorphDictionaryAPI.ME_OPENCORPORA_DICTIONARY_VARIANT,
            cfg.variant);
    String dictEntryName = String.format(
            OpencorporaMorphDictionaryAPI.FILENAME_PATTERN_OPENCORPORA_SERIALIZED_DICT, dict.getVersion(),
            dict.getRevision(), cfg.variant);
    JarOutputStream jarOut = new JarOutputStream(fout, manifest);
    jarOut.putNextEntry(new ZipEntry(dictEntryName));
    ObjectOutputStream serOut = new ObjectOutputStream(jarOut);
    try {
        serOut.writeObject(dict.getGramModel());
        serOut.writeObject(dict);
    } finally {
        serOut.flush();
        jarOut.closeEntry();
        serOut.close();
    }
    System.out.println(String.format("Serialization finished in %s ms.\nOutput size: %s bytes",
            currentTimeMillis() - timeBefore, cfg.outputJarFile.length()));
}

From source file:org.jahia.modules.serversettings.portlets.BasePortletHelper.java

static void addToJar(String resource, String targetJarEntry, JarOutputStream jos) throws IOException {
    jos.putNextEntry(new JarEntry(targetJarEntry));
    InputStream is = BasePortletHelper.class.getClassLoader().getResourceAsStream(resource);
    try {/*from   w w w  . j  a  v  a 2 s.c  o m*/
        IOUtils.copy(is, jos);
        jos.closeEntry();
    } finally {
        IOUtils.closeQuietly(is);
    }
}

From source file:org.apache.blur.spark.util.JavaSparkUtil.java

private static void pack(File rootPath, File source, JarOutputStream target) throws IOException {
    String name = getName(rootPath, source);
    if (source.isDirectory()) {
        if (!SEP.equals(name)) {
            JarEntry entry = new JarEntry(name);
            entry.setTime(source.lastModified());
            target.putNextEntry(entry);//from   ww  w.  j a  v  a 2  s. com
            target.closeEntry();
        }
        for (File f : source.listFiles()) {
            pack(rootPath, f, target);
        }
    } else {
        JarEntry entry = new JarEntry(name);
        entry.setTime(source.lastModified());
        target.putNextEntry(entry);
        BufferedInputStream in = new BufferedInputStream(new FileInputStream(source));
        IOUtils.copy(in, target);
        in.close();
        target.closeEntry();
    }
}

From source file:com.samczsun.helios.utils.Utils.java

public static void save(File dest, Map<String, byte[]> data, Predicate<String> accept) {
    try {//from ww  w.jav  a  2  s  .  co m
        JarOutputStream out = new JarOutputStream(new FileOutputStream(dest));
        Set<String> added = new HashSet<>();
        for (Entry<String, byte[]> entry : data.entrySet()) {
            String name = entry.getKey();
            if (added.add(name) && accept.test(name)) {
                out.putNextEntry(new ZipEntry(name));
                out.write(entry.getValue());
                out.closeEntry();
            }
        }
        out.close();
    } catch (IOException e) {
        ExceptionHandler.handle(e);
    }
}

From source file:com.digitalreasoning.herman.JarCreater.java

public static void createJar(File outputJarFile, List<Entry> entries) throws IOException {
    if (!outputJarFile.getParentFile().exists()) {
        outputJarFile.getParentFile().mkdirs();
    }/*  w  w  w . j a v  a  2  s. c  om*/
    if (!outputJarFile.exists()) {
        outputJarFile.createNewFile();
    }

    FileOutputStream fOut = new FileOutputStream(outputJarFile);
    JarOutputStream jarOut = new JarOutputStream(fOut);
    Set<String> packageSet = new HashSet<String>();
    try {
        for (Entry folderFile : entries) {
            InputStream inputStream = folderFile.resource.openStream();

            try {
                if (!packageSet.contains(folderFile.parentFolderName)) {
                    jarOut.putNextEntry(new ZipEntry(folderFile.parentFolderName));
                    jarOut.closeEntry();
                    packageSet.add(folderFile.parentFolderName);
                }

                jarOut.putNextEntry(new ZipEntry(folderFile.parentFolderName
                        + (folderFile.parentFolderName.endsWith("/") ? "" : "/") + folderFile.fileName));
                IOUtils.copy(inputStream, jarOut);
                jarOut.closeEntry();
            } finally {
                inputStream.close();
            }

        }

    } finally {
        jarOut.close();
        fOut.close();
    }
}

From source file:org.sourcepit.common.maven.testing.ArtifactRepositoryFacade.java

private static File createStubJar(File dir) throws IOException {
    final File jarFile = File.createTempFile("stub", ".jar", dir);

    JarOutputStream jarOut = null;
    try {//w  ww  .j av  a2 s.  c o m
        jarOut = new JarOutputStream(new FileOutputStream(jarFile));

        final JarEntry mfEntry = new JarEntry(JarFile.MANIFEST_NAME);
        jarOut.putNextEntry(mfEntry);

        final Manifest mf = new Manifest();
        mf.getMainAttributes().put(Attributes.Name.MANIFEST_VERSION, "1");
        mf.write(jarOut);

        jarOut.closeEntry();
    } finally {
        IOUtils.closeQuietly(jarOut);
    }

    return jarFile;
}

From source file:org.apache.sling.tooling.support.install.impl.InstallServlet.java

private static void createJar(final File sourceDir, final File jarFile, final Manifest mf) throws IOException {
    final JarOutputStream zos = new JarOutputStream(new FileOutputStream(jarFile));
    try {/* w w  w  .  j  a  v  a2 s  .co  m*/
        zos.setLevel(Deflater.NO_COMPRESSION);
        // manifest first
        final ZipEntry anEntry = new ZipEntry(JarFile.MANIFEST_NAME);
        zos.putNextEntry(anEntry);
        mf.write(zos);
        zos.closeEntry();
        zipDir(sourceDir, zos, "");
    } finally {
        try {
            zos.close();
        } catch (final IOException ignore) {
            // ignore
        }
    }
}

From source file:org.eclipse.wb.tests.designer.TestUtils.java

/**
 * @return the path to the temporary "jar" file with single entry.
 * /* w ww  .ja va  2 s . c  o  m*/
 * @param entryName
 *          the name of entry, for example <code>"myFolder/subFolder/file.txt"</code>.
 * @param content
 *          the {@link String} content of entry.
 */
public static String createTemporaryJar(String entryName, String content) throws Exception {
    File tempFile = File.createTempFile("wbpTests", ".jar");
    tempFile.deleteOnExit();
    // create "jar" with single entry
    {
        JarOutputStream jarOutputStream = new JarOutputStream(new FileOutputStream(tempFile));
        jarOutputStream.putNextEntry(new ZipEntry(entryName));
        jarOutputStream.write(content.getBytes());
        jarOutputStream.closeEntry();
        jarOutputStream.close();
    }
    // return path to "jar"
    return tempFile.getAbsolutePath();
}

From source file:org.sourcepit.osgifier.core.packaging.BundleLocalizationWriter.java

public static Map<String, LocalizedData> write(final JarOutputStream out, Manifest manifest,
        BundleLocalization localization) throws IOException {
    final BundleLocalizationWriter<JarOutputStream> writer = new BundleLocalizationWriter<JarOutputStream>() {
        @Override/*  ww  w  .j a v  a  2 s  . co m*/
        protected JarOutputStream openStream(String path) throws IOException {
            out.putNextEntry(new JarEntry(path));
            return out;
        }

        @Override
        protected void closeStream(JarOutputStream out, String path) throws IOException {
            out.closeEntry();
        }
    };
    return writer.write(manifest, localization);
}