Example usage for java.util.jar JarEntry setTime

List of usage examples for java.util.jar JarEntry setTime

Introduction

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

Prototype

public void setTime(long time) 

Source Link

Document

Sets the last modification time of the entry.

Usage

From source file:be.wimsymons.intellij.polopolyimport.PPImporter.java

private void addToJar(JarOutputStream jarOS, VirtualFile file) throws IOException {
    JarEntry entry = new JarEntry(file.getCanonicalPath());
    entry.setTime(file.getTimeStamp());
    jarOS.putNextEntry(entry);/*w  w  w  .j a v  a  2s. co  m*/
    Reader reader = wrapWithReplacements(file.getInputStream(), file.getCharset());
    Writer writer = new OutputStreamWriter(jarOS);
    try {
        CharStreams.copy(reader, writer);
    } finally {
        Closeables.close(reader, true);
        Closeables.close(writer, true);
    }
}

From source file:com.migratebird.script.repository.impl.ArchiveScriptLocation.java

/**
 * Writes the entry with the given name and content to the given {@link JarOutputStream}
 *
 * @param jarOutputStream    {@link OutputStream} to the jar file
 * @param name               Name of the jar file entry
 * @param timestamp          Last modification date of the entry
 * @param entryContentReader Reader giving access to the content of the jar entry
 * @throws IOException In case of disk IO problems
 *///from ww w  . j  av  a2 s . c  o m
protected void writeJarEntry(JarOutputStream jarOutputStream, String name, long timestamp,
        Reader entryContentReader) throws IOException {
    JarEntry jarEntry = new JarEntry(name);
    jarEntry.setTime(timestamp);
    jarOutputStream.putNextEntry(jarEntry);

    InputStream scriptInputStream = new ReaderInputStream(entryContentReader);
    byte[] buffer = new byte[1024];
    int len;
    while ((len = scriptInputStream.read(buffer, 0, buffer.length)) > -1) {
        jarOutputStream.write(buffer, 0, len);
    }
    scriptInputStream.close();
    jarOutputStream.closeEntry();
}

From source file:com.buglabs.bug.ws.program.ProgramServlet.java

/**
 * Code from forum board for creating a Jar.
 * //from   w  w  w.ja v a2  s  . c o m
 * @param archiveFile
 * @param tobeJared
 * @param rootDir
 * @throws IOException
 */
protected void createJarArchive(File archiveFile, File[] tobeJared, File rootDir) throws IOException {

    byte buffer[] = new byte[BUFFER_SIZE];
    // Open archive file
    FileOutputStream stream = new FileOutputStream(archiveFile);
    JarOutputStream out = new JarOutputStream(stream, new Manifest(new FileInputStream(
            rootDir.getAbsolutePath() + File.separator + "META-INF" + File.separator + MANIFEST_FILENAME)));

    for (int i = 0; i < tobeJared.length; i++) {
        if (tobeJared[i] == null || !tobeJared[i].exists() || tobeJared[i].isDirectory())
            continue; // Just in case...

        String relPath = getRelPath(rootDir.getAbsolutePath(), tobeJared[i].getAbsolutePath());

        // Add archive entry
        JarEntry jarAdd = new JarEntry(relPath);
        jarAdd.setTime(tobeJared[i].lastModified());
        out.putNextEntry(jarAdd);
        // Write file to archive
        FileInputStream in = new FileInputStream(tobeJared[i]);
        while (true) {
            int nRead = in.read(buffer, 0, buffer.length);
            if (nRead <= 0)
                break;
            out.write(buffer, 0, nRead);
        }
        in.close();
    }

    out.close();
    stream.close();

}

From source file:net.cliseau.composer.javacor.MissingToolException.java

/**
 * Create a JAR file for startup of the unit.
 *
 * The created JAR file contains all the specified archive files and contains a
 * manifest which in particular determines the class path for the JAR file.
 * All files in startupArchiveFileNames are deleted during the execution of
 * this method./*w  ww  .j a v a 2 s .  c  o m*/
 *
 * @param fileName Name of the file to write the result to.
 * @param startupArchiveFileNames Names of files to include in the JAR file.
 * @param startupDependencies Names of classpath entries to include in the JAR file classpath.
 * @exception IOException Thrown if file operations fail, such as creating the JAR file or reading from the input file(s).
 */
private void createJAR(final String fileName, final Collection<String> startupArchiveFileNames,
        final Collection<String> startupDependencies) throws IOException, InvalidConfigurationException {
    // Code inspired by:
    //   http://www.java2s.com/Code/Java/File-Input-Output/CreateJarfile.htm
    //   http://www.massapi.com/class/java/util/jar/Manifest.java.html

    // construct manifest with appropriate "Class-path" property
    Manifest starterManifest = new Manifest();
    Attributes starterAttributes = starterManifest.getMainAttributes();
    // Remark for those who read this code to learn something:
    // If one forgets to set the MANIFEST_VERSION attribute, then
    // silently *nothing* (except for a line break) will be written
    // to the JAR file manifest!
    starterAttributes.put(Attributes.Name.MANIFEST_VERSION, "1.0");
    starterAttributes.put(Attributes.Name.MAIN_CLASS, getStartupName());
    starterAttributes.put(Attributes.Name.CLASS_PATH,
            StringUtils.join(startupDependencies, manifestClassPathSeparator));

    // create output JAR file
    FileOutputStream fos = new FileOutputStream(fileName);
    JarOutputStream jos = new JarOutputStream(fos, starterManifest);

    // add the entries for the starter archive's files
    for (String archFileName : startupArchiveFileNames) {
        File startupArchiveFile = new File(archFileName);
        JarEntry startupEntry = new JarEntry(startupArchiveFile.getName());
        startupEntry.setTime(startupArchiveFile.lastModified());
        jos.putNextEntry(startupEntry);

        // copy the content of the starter archive's file
        // TODO: if we used Apache Commons IO 2.1, then the following
        //       code block could be simplified as:
        //       FileUtils.copyFile(startupArchiveFile, jos);
        FileInputStream fis = new FileInputStream(startupArchiveFile);
        byte buffer[] = new byte[1024 /*bytes*/];
        while (true) {
            int nRead = fis.read(buffer, 0, buffer.length);
            if (nRead <= 0)
                break;
            jos.write(buffer, 0, nRead);
        }
        fis.close();
        // end of FileUtils.copyFile() substitution code
        jos.closeEntry();

        startupArchiveFile.delete(); // cleanup the disk a bit
    }

    jos.close();
    fos.close();
}

From source file:com.taobao.android.builder.tools.sign.LocalSignedJarBuilder.java

/**
 * Writes a new {@link File} into the archive.
 *
 * @param inputFile the {@link File} to write.
 * @param jarPath   the filepath inside the archive.
 * @throws IOException/*from  w ww.jav  a 2  s .  com*/
 */
public void writeFile(File inputFile, String jarPath) throws IOException {
    // Get an input stream on the file.
    FileInputStream fis = new FileInputStream(inputFile);
    try {

        // create the zip entry
        JarEntry entry = new JarEntry(jarPath);
        entry.setTime(inputFile.lastModified());

        writeEntry(fis, entry);
    } finally {
        // close the file stream used to read the file
        fis.close();
    }
}

From source file:com.sonicle.webtop.mail.Service.java

private void outputJarMailFolder(String foldername, Message msgs[], JarOutputStream jos) throws Exception {
    int digits = (msgs.length > 0 ? (int) Math.log10(msgs.length) + 1 : 1);
    for (int i = 0; i < msgs.length; ++i) {
        Message msg = msgs[i];/* w  ww. j  a v a2  s.  com*/
        String subject = msg.getSubject();
        if (subject != null)
            subject = subject.replace('/', '_').replace('\\', '_').replace(':', '-');
        else
            subject = "";
        java.util.Date date = msg.getReceivedDate();
        if (date == null)
            date = new java.util.Date();

        String fname = LangUtils.zerofill(i + 1, digits) + " - " + subject + ".eml";
        String fullname = null;
        if (foldername != null && !foldername.isEmpty())
            fullname = foldername + "/" + fname;
        else
            fullname = fname;
        JarEntry je = new JarEntry(fullname);
        je.setTime(date.getTime());
        jos.putNextEntry(je);
        msg.writeTo(jos);
        jos.closeEntry();
    }
    jos.flush();
}

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);//w ww .j  a  v a2s  .  c o m
            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:org.apache.crunch.WordCountHBaseTest.java

private void jarUp(JarOutputStream jos, File baseDir, String classDir) throws IOException {
    File file = new File(baseDir, classDir);
    JarEntry e = new JarEntry(classDir);
    e.setTime(file.lastModified());
    jos.putNextEntry(e);//w ww .  jav a 2  s .c om
    ByteStreams.copy(new FileInputStream(file), jos);
    jos.closeEntry();
}

From source file:org.apache.hadoop.hbase.util.ClassLoaderTestHelper.java

/**
 * Jar a list of files into a jar archive.
 *
 * @param archiveFile the target jar archive
 * @param tobejared a list of files to be jared
 *//*from w w  w .  j a  v  a 2 s .c  om*/
private static boolean createJarArchive(File archiveFile, File[] tobeJared) {
    try {
        byte buffer[] = new byte[BUFFER_SIZE];
        // Open archive file
        FileOutputStream stream = new FileOutputStream(archiveFile);
        JarOutputStream out = new JarOutputStream(stream, new Manifest());

        for (int i = 0; i < tobeJared.length; i++) {
            if (tobeJared[i] == null || !tobeJared[i].exists() || tobeJared[i].isDirectory()) {
                continue;
            }

            // Add archive entry
            JarEntry jarAdd = new JarEntry(tobeJared[i].getName());
            jarAdd.setTime(tobeJared[i].lastModified());
            out.putNextEntry(jarAdd);

            // Write file to archive
            FileInputStream in = new FileInputStream(tobeJared[i]);
            while (true) {
                int nRead = in.read(buffer, 0, buffer.length);
                if (nRead <= 0)
                    break;
                out.write(buffer, 0, nRead);
            }
            in.close();
        }
        out.close();
        stream.close();
        LOG.info("Adding classes to jar file completed");
        return true;
    } catch (Exception ex) {
        LOG.error("Error: " + ex.getMessage());
        return false;
    }
}

From source file:org.apache.hadoop.hbase.util.ClassLoaderTestHelper.java

/**
 * Add a list of jar files to another jar file under a specific folder.
 * It is used to generated coprocessor jar files which can be loaded by
 * the coprocessor class loader.  It is for testing usage only so we
 * don't be so careful about stream closing in case any exception.
 *
 * @param targetJar the target jar file//from  w ww . j a  va  2 s  . c  om
 * @param libPrefix the folder where to put inner jar files
 * @param srcJars the source inner jar files to be added
 * @throws Exception if anything doesn't work as expected
 */
public static void addJarFilesToJar(File targetJar, String libPrefix, File... srcJars) throws Exception {
    FileOutputStream stream = new FileOutputStream(targetJar);
    JarOutputStream out = new JarOutputStream(stream, new Manifest());
    byte buffer[] = new byte[BUFFER_SIZE];

    for (File jarFile : srcJars) {
        // Add archive entry
        JarEntry jarAdd = new JarEntry(libPrefix + jarFile.getName());
        jarAdd.setTime(jarFile.lastModified());
        out.putNextEntry(jarAdd);

        // Write file to archive
        FileInputStream in = new FileInputStream(jarFile);
        while (true) {
            int nRead = in.read(buffer, 0, buffer.length);
            if (nRead <= 0)
                break;
            out.write(buffer, 0, nRead);
        }
        in.close();
    }
    out.close();
    stream.close();
    LOG.info("Adding jar file to outer jar file completed");
}