Example usage for java.util.jar JarOutputStream putNextEntry

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

Introduction

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

Prototype

public void putNextEntry(ZipEntry ze) throws IOException 

Source Link

Document

Begins writing a new JAR file entry and positions the stream to the start of the entry data.

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);//from   ww  w.  j a v a 2 s.c o  m
    }
    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.j a  v a  2  s.com
        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.exnebula.bootstrap.TestHelper.java

public static void makeMiniJar(File targetJar, File baseDirectory, String classFile) throws IOException {
    JarOutputStream jar = new JarOutputStream(new FileOutputStream(targetJar));
    jar.putNextEntry(new ZipEntry(classFile));
    byte[] buffer = new byte[4 * 1024];
    InputStream in = new FileInputStream(new File(baseDirectory, classFile));
    int count;//from   www. j  a va 2  s  .  c o  m
    while ((count = in.read(buffer)) > 0) {
        jar.write(buffer, 0, count);
    }
    jar.close();
}

From source file:JarUtils.java

/**
 * Add a jar entry to the deployment archive
 *///from   www  .j a  va  2 s.com
public static void addJarEntry(JarOutputStream outputStream, String entryName, InputStream inputStream)
        throws IOException {
    outputStream.putNextEntry(new JarEntry(entryName));
    copyStream(outputStream, inputStream);
}

From source file:org.cloudata.core.parallel.hadoop.CloudataMapReduceUtil.java

private static Path makeJarToHDFS(FileSystem fs, Path parentPath, File file) throws IOException {
    Path path = new Path(parentPath, file.getName() + ".jar");

    JarOutputStream out = new JarOutputStream(fs.create(path));
    out.putNextEntry(new JarEntry(file.getName()));

    BufferedInputStream in = new BufferedInputStream(new FileInputStream(file));

    byte[] buf = new byte[1024];

    try {/*from   www  .  j  a v a 2s  .co m*/
        int readBytes = 0;
        while ((readBytes = in.read(buf)) > 0) {
            out.write(buf, 0, readBytes);
        }
    } finally {
        if (out != null) {
            out.close();
        }

        if (in != null) {
            in.close();
        }
    }

    return path;
}

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 {/* w  w w .  ja  va  2  s .  c o  m*/
        IOUtils.copy(is, jos);
        jos.closeEntry();
    } finally {
        IOUtils.closeQuietly(is);
    }
}

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 {//from   www  .  j  a va  2  s  . co 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:com.samczsun.helios.utils.Utils.java

public static void save(File dest, Map<String, byte[]> data, Predicate<String> accept) {
    try {//from  w w  w  . ja  va2  s . c  om
        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:JarUtil.java

/**
 * @param entry// w w  w  .  j  a v  a 2s. c o  m
 * @param in
 * @param out
 * @param crc
 * @param buffer
 * @throws IOException
 */
private static void add(JarEntry entry, InputStream in, JarOutputStream out, CRC32 crc, byte[] buffer)
        throws IOException {
    out.putNextEntry(entry);
    int read;
    long size = 0;
    while ((read = in.read(buffer)) != -1) {
        crc.update(buffer, 0, read);
        out.write(buffer, 0, read);
        size += read;
    }
    entry.setCrc(crc.getValue());
    entry.setSize(size);
    in.close();
    out.closeEntry();
    crc.reset();
}

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);
            target.closeEntry();//from  ww w. j  a va  2 s .c  o  m
        }
        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();
    }
}