Package java.util.jar

Examples of java.util.jar.JarOutputStream


public final class ExecutableConfigurationUtil {
    private ExecutableConfigurationUtil() {
    }

    public static void createExecutableConfiguration(ConfigurationData configurationData, Manifest manifest, File configurationDir, File destinationFile) throws IOException, InvalidConfigException {
        JarOutputStream out = null;
        try {
            byte[] buffer = new byte[4096];

            if (manifest != null) {
                out = new JarOutputStream(new FileOutputStream(destinationFile), manifest);

                // add the startup file which allows us to locate the startup directory
                out.putNextEntry(new ZipEntry("META-INF/startup-jar"));
                out.closeEntry();
            } else {
                out = new JarOutputStream(new FileOutputStream(destinationFile));
            }

            // write the configurationData
            ExecutableConfigurationUtil.writeConfiguration(configurationData, out);

            URI baseURI = configurationDir.getAbsoluteFile().toURI();
            Collection files = listRecursiveFiles(configurationDir);
            for (Iterator iterator = files.iterator(); iterator.hasNext();) {
                File file = (File) iterator.next();
                String relativePath = baseURI.relativize(file.toURI()).getPath();
                InputStream in = new FileInputStream(file);
                try {
                    out.putNextEntry(new ZipEntry(relativePath));
                    try {
                        int count;
                        while ((count = in.read(buffer)) > 0) {
                            out.write(buffer, 0, count);
                        }
                    } finally {
                        out.closeEntry();
                    }
                } finally {
                    close(in);
                }
            }
View Full Code Here


        ReadOnlyRepository repository = new ReadOnlyRepository(localRepo);
        ServiceConfigBuilder builder = new ServiceConfigBuilder(null, repository);
        ConfigurationData configurationData = builder.buildConfiguration(config, null, buildDir);

        JarOutputStream out = new JarOutputStream(new FileOutputStream(carFile));
        ExecutableConfigurationUtil.writeConfiguration(configurationData, out);
        out.flush();
        out.close();
    }
View Full Code Here

            gbean.setAttribute("gBeanState", state);


            sourceFile = File.createTempFile("test", ".car");
            source = sourceFile.toURL();
            JarOutputStream jos = new JarOutputStream(new BufferedOutputStream(new FileOutputStream(sourceFile)));
            jos.putNextEntry(new ZipEntry("META-INF/config.ser"));
            ObjectOutputStream oos = new ObjectOutputStream(jos);
            gbean.writeExternal(oos);
            oos.flush();
            jos.closeEntry();
            jos.close();
        } catch (Exception e) {
            if (sourceFile != null) {
                sourceFile.delete();
            }
            throw e;
View Full Code Here

                // out target is just a plain old jar file directly accessabel from the file system
                copyFile(new File(baseJar.getName()), outputFile);
            }
        } else {
            // copy out the module contents to a standalone jar file (entry by entry)
            JarOutputStream out = null;
            try {
                out = new JarOutputStream(new FileOutputStream(outputFile));
                byte[] buffer = new byte[4096];
                Enumeration entries = inputJar.entries();
                while (entries.hasMoreElements()) {
                    ZipEntry entry = (ZipEntry) entries.nextElement();
                    InputStream in = inputJar.getInputStream(entry);
                    try {
                        out.putNextEntry(new ZipEntry(entry.getName()));
                        try {
                            int count;
                            while ((count = in.read(buffer)) > 0) {
                                out.write(buffer, 0, count);
                            }
                        } finally {
                            out.closeEntry();
                        }
                    } finally {
                        close(in);
                    }
                }
View Full Code Here

        int pos = fileName.indexOf(oldVersion);
        fileName = fileName.substring(0, pos) + newVersion
            + fileName.substring(pos + oldVersion.length());

        JarInputStream jis = null;
        JarOutputStream jos;
        OutputStream out = null;
        try {
            // now create a temporary file and update the version
            final JarFile sourceJar = new JarFile(file);
            final Manifest manifest = sourceJar.getManifest();
            manifest.getMainAttributes().putValue("Bundle-Version", newVersion);

            jis = new JarInputStream(new FileInputStream(file));
            final File destJar = new File(file.getParentFile(), fileName);
            out = new FileOutputStream(destJar);
            jos = new JarOutputStream(out, manifest);

            jos.setMethod(JarOutputStream.DEFLATED);
            jos.setLevel(Deflater.BEST_COMPRESSION);

            JarEntry entryIn = jis.getNextJarEntry();
            while (entryIn != null) {
                JarEntry entryOut = new JarEntry(entryIn.getName());
                entryOut.setTime(entryIn.getTime());
                entryOut.setComment(entryIn.getComment());
                jos.putNextEntry(entryOut);
                if (!entryIn.isDirectory()) {
                    IOUtils.copy(jis, jos);
                }
                jos.closeEntry();
                jis.closeEntry();
                entryIn = jis.getNextJarEntry();
            }

            // close the JAR file now to force writing
            jos.close();
            return destJar;
        } catch (IOException ioe) {
            throw new MojoExecutionException(
                "Unable to update version in jar file.", ioe);
        } finally {
View Full Code Here

        return found;
    }

    private static void createJar(final File sourceDir, final File jarFile, final Manifest mf)
    throws IOException {
        final JarOutputStream zos = new JarOutputStream(new FileOutputStream(jarFile));
        try {
            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
            }
        }
    }
View Full Code Here

    File spoolModified(InputStream ins) throws IOException {
        JarInputStream jis = new JarInputStream(ins);

        // immediately handle the manifest
        JarOutputStream jos;
        Manifest manifest = jis.getManifest();
        if (manifest == null) {
            throw new IOException("Missing Manifest !");
        }

        String symbolicName = manifest.getMainAttributes().getValue(
            "Bundle-SymbolicName");
        if (symbolicName == null || symbolicName.length() == 0) {
            throw new IOException("Missing Symbolic Name in Manifest !");
        }

        String version = manifest.getMainAttributes().getValue("Bundle-Version");
        Version v = Version.parseVersion(version);
        if (v.getQualifier().indexOf("SNAPSHOT") >= 0) {
            String tStamp;
            synchronized (DATE_FORMAT) {
                tStamp = DATE_FORMAT.format(new Date());
            }
            version = v.getMajor() + "." + v.getMinor() + "." + v.getMicro()
                + "." + v.getQualifier().replaceAll("SNAPSHOT", tStamp);
            manifest.getMainAttributes().putValue("Bundle-Version", version);
        }

        File bundle = new File(this.repoLocation, symbolicName + "-" + v + ".jar");
        OutputStream out = null;
        try {
            out = new FileOutputStream(bundle);
            jos = new JarOutputStream(out, manifest);

            jos.setMethod(JarOutputStream.DEFLATED);
            jos.setLevel(Deflater.BEST_COMPRESSION);

            JarEntry entryIn = jis.getNextJarEntry();
            while (entryIn != null) {
                JarEntry entryOut = new JarEntry(entryIn.getName());
                entryOut.setTime(entryIn.getTime());
                entryOut.setComment(entryIn.getComment());
                jos.putNextEntry(entryOut);
                if (!entryIn.isDirectory()) {
                    spool(jis, jos);
                }
                jos.closeEntry();
                jis.closeEntry();
                entryIn = jis.getNextJarEntry();
            }

            // close the JAR file now to force writing
            jos.close();

        } finally {
            IOUtils.closeQuietly(out);
        }
View Full Code Here

      if(mainClazz != null) {
        manifest.getMainAttributes().put(Attributes.Name.MAIN_CLASS, mainClazz.getName());
      }
      manifest.getMainAttributes().put(new Attributes.Name("X-Rascal-Saved-Class"), clazz.getName());
      JarOutputStream target = new JarOutputStream(outStream, manifest);
      JarEntry entry = new JarEntry("META-INF/");
      target.putNextEntry(entry);
      Collection<String> dirs = new ArrayList<String>();

      for (JavaFileObject o : list) {
        String path = o.toUri().getPath().replace(".", "/");
        makeJarDirs(target, dirs, path);
        entry = new JarEntry(path + ".class");
        entry.setTime(o.getLastModified());
        target.putNextEntry(entry);
       
        try(InputStream stream = o.openInputStream()) {
          byte[] buffer = new byte[8192];
          int c = stream.read(buffer);
          while (c > -1) {
            target.write(buffer, 0, c);
            c = stream.read(buffer);
          }
        }
        target.closeEntry();

      }
     
      if(mainClazz != null) {
        String name = mainClazz.getName();
        String path = name.replace(".", "/") + ".class";
       
        String dir = path.substring(0, path.lastIndexOf('/'));
        StringBuilder dirTmp = new StringBuilder(dir.length());
        for (String d : dir.split("/")) {
          dirTmp.append(d);
          dirTmp.append("/");
          String tmp = dirTmp.toString();
          if (!dirs.contains(tmp)) {
            dirs.add(tmp);
            entry = new JarEntry(tmp);
            target.putNextEntry(entry);
          }
        }
        entry = new JarEntry(path);
        target.putNextEntry(entry);
       
        try(InputStream stream = mainClazz.getClassLoader().getResourceAsStream(path)) {
          byte[] buffer = new byte[8192];
          int c = stream.read(buffer);
          while (c > -1) {
            target.write(buffer, 0, c);
            c = stream.read(buffer);
          }
        }
        target.closeEntry();
      }

      target.close();
    }

  }
View Full Code Here

                  InputStream classStream = classLoader.getResourceAsStream(classLocation);
                  assertNotNull("Cannot find test class", classStream);

                  // prepare the in-memory JAR file
                  ByteArrayOutputStream byteArrayStream = new ByteArrayOutputStream();
                  JarOutputStream jarOutput = new JarOutputStream(byteArrayStream);

                  // write the class file to the archive
                  jarOutput.putNextEntry(new ZipEntry(classLocation));
                  byte[] buffer = new byte[255];
                  for (int len; (len = classStream.read(buffer)) != -1;)
                  {
                     jarOutput.write(buffer, 0, len);
                  }
                  jarOutput.closeEntry();

                  // close the JAR archive and return the InputStream
                  jarOutput.close();
                  return new ByteArrayInputStream(byteArrayStream.toByteArray());

               }
               catch (IOException e)
               {
View Full Code Here

                }
                e.getParentNode().removeChild(e);
            }
        }

        JarOutputStream out = new JarOutputStream(os);
        ZipEntry e = new ZipEntry(JarFile.MANIFEST_NAME);
        out.putNextEntry(e);
        m.write(out);
        out.closeEntry();
        e = new ZipEntry("OSGI-INF/");
        out.putNextEntry(e);
        e = new ZipEntry("OSGI-INF/blueprint/");
        out.putNextEntry(e);
        out.closeEntry();
        // check .xml file extension
        if( !name.endsWith(".xml")) {
            name +=".xml";
        }
        e = new ZipEntry("OSGI-INF/blueprint/" + name);
        out.putNextEntry(e);
        // Copy the new DOM
        if (tf == null) {
            tf = TransformerFactory.newInstance();
        }
        tf.newTransformer().transform(new DOMSource(doc), new StreamResult(out));
        out.closeEntry();
        out.close();
    }
View Full Code Here

TOP

Related Classes of java.util.jar.JarOutputStream

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.