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:Main.java

void createJarArchive(File archiveFile, File[] tobeJared) throws Exception {
    byte buffer[] = new byte[BUFFER_SIZE];
    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; // Just in case...
        JarEntry jarAdd = new JarEntry(tobeJared[i].getName());
        jarAdd.setTime(tobeJared[i].lastModified());
        out.putNextEntry(jarAdd);/*from w  ww  .  j a v a 2  s .  co m*/
        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:CreateJarFile.java

protected void createJarArchive(File archiveFile, File[] tobeJared) {
    try {//from  w  ww.ja v  a  2  s . c o  m
        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; // Just in case...
            System.out.println("Adding " + tobeJared[i].getName());

            // 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();
        System.out.println("Adding completed OK");
    } catch (Exception ex) {
        ex.printStackTrace();
        System.out.println("Error: " + ex.getMessage());
    }
}

From source file:com.scorpio4.util.io.JarArchiver.java

public String add(InputStream inputStream, String filename, String comment, long size)
        throws IOException, NoSuchAlgorithmException {
    if (jarOutputStream == null)
        throw new IOException("not open");
    JarEntry entry = new JarEntry(filename);
    entry.setSize(size);// ww w .  ja  v a  2  s  .  c  om

    entry.setMethod(JarEntry.DEFLATED); // compressed
    entry.setTime(System.currentTimeMillis());
    if (comment != null)
        entry.setComment(comment);
    jarOutputStream.putNextEntry(entry);

    // copy I/O & return SHA-1 fingerprint
    String fingerprint = Fingerprint.copy(inputStream, jarOutputStream, 4096);
    IOUtils.copy(inputStream, jarOutputStream);
    log.debug("JAR add " + size + " bytes: " + filename + " -> " + fingerprint);

    jarOutputStream.closeEntry();
    //      jarOutputStream.finish();
    return fingerprint;
}

From source file:JarHelper.java

/**
 * Recursively jars up the given path under the given directory.
 */// w  ww. j a v  a2  s  .  c om
private void jarDir(File dirOrFile2jar, JarOutputStream jos, String path) throws IOException {
    if (mVerbose)
        System.out.println("checking " + dirOrFile2jar);
    if (dirOrFile2jar.isDirectory()) {
        String[] dirList = dirOrFile2jar.list();
        String subPath = (path == null) ? "" : (path + dirOrFile2jar.getName() + SEP);
        if (path != null) {
            JarEntry je = new JarEntry(subPath);
            je.setTime(dirOrFile2jar.lastModified());
            jos.putNextEntry(je);
            jos.flush();
            jos.closeEntry();
        }
        for (int i = 0; i < dirList.length; i++) {
            File f = new File(dirOrFile2jar, dirList[i]);
            jarDir(f, jos, subPath);
        }
    } else {
        if (dirOrFile2jar.getCanonicalPath().equals(mDestJarName)) {
            if (mVerbose)
                System.out.println("skipping " + dirOrFile2jar.getPath());
            return;
        }

        if (mVerbose)
            System.out.println("adding " + dirOrFile2jar.getPath());
        FileInputStream fis = new FileInputStream(dirOrFile2jar);
        try {
            JarEntry entry = new JarEntry(path + dirOrFile2jar.getName());
            entry.setTime(dirOrFile2jar.lastModified());
            jos.putNextEntry(entry);
            while ((mByteCount = fis.read(mBuffer)) != -1) {
                jos.write(mBuffer, 0, mByteCount);
                if (mVerbose)
                    System.out.println("wrote " + mByteCount + " bytes");
            }
            jos.flush();
            jos.closeEntry();
        } catch (IOException ioe) {
            throw ioe;
        } finally {
            fis.close();
        }
    }
}

From source file:gov.redhawk.rap.internal.PluginProviderServlet.java

private void addFiles(final JarOutputStream out, final IPath path, final File... listFiles) throws IOException {
    for (final File f : listFiles) {
        if (f.getName().charAt(0) == '.') {
            continue;
        } else if (f.getName().equals("MANIFEST.MF")) {
            continue;
        }/*from ww  w  .ja  va  2  s  . c o m*/
        final IPath filePath = path.append(f.getName());
        String str = filePath.toString();
        if (str.startsWith("/")) {
            str = str.substring(1);
        }
        if (f.isDirectory()) {
            if (!str.endsWith("/")) {
                str = str + "/";
            }
            final JarEntry entry = new JarEntry(str);
            entry.setSize(0);
            entry.setTime(f.lastModified());
            out.putNextEntry(entry);
            out.closeEntry();
            addFiles(out, filePath, f.listFiles());
        } else {
            final JarEntry entry = new JarEntry(str);
            entry.setSize(f.length());
            entry.setTime(f.lastModified());
            out.putNextEntry(entry);
            final FileInputStream istream = new FileInputStream(f);
            try {
                IOUtils.copy(istream, out);
            } finally {
                istream.close();
            }
            out.closeEntry();
        }
    }
}

From source file:gov.nih.nci.restgen.util.JarHelper.java

/**
 * Recursively jars up the given path under the given directory.
 *//*  w w  w.  j  av  a 2  s.c o m*/
private void jarDir(File dirOrFile2jar, JarOutputStream jos, String path) throws IOException {
    //if (mVerbose)
    {
        //System.out.println("checking " + dirOrFile2jar);
    }
    if (dirOrFile2jar.isDirectory()) {
        String[] dirList = dirOrFile2jar.list();
        String subPath = (path == null) ? "" : (path + dirOrFile2jar.getName() + SEP);
        if (path != null) {
            JarEntry je = new JarEntry(subPath);
            je.setTime(dirOrFile2jar.lastModified());
            jos.putNextEntry(je);
            jos.flush();
            jos.closeEntry();
        }
        for (int i = 0; i < dirList.length; i++) {
            File f = new File(dirOrFile2jar, dirList[i]);
            jarDir(f, jos, subPath);
        }
    } else {
        if (dirOrFile2jar.getCanonicalPath().equals(mDestJarName)) {
            //if (mVerbose)
            {
                //System.out.println("skipping " + dirOrFile2jar.getPath());
            }
            return;
        }

        if (mVerbose) {
            //System.out.println("adding " + dirOrFile2jar.getPath());
        }
        FileInputStream fis = new FileInputStream(dirOrFile2jar);
        try {
            JarEntry entry = new JarEntry(path + dirOrFile2jar.getName());
            entry.setTime(dirOrFile2jar.lastModified());
            jos.putNextEntry(entry);
            while ((mByteCount = fis.read(mBuffer)) != -1) {
                jos.write(mBuffer, 0, mByteCount);
                if (mVerbose) {
                    //System.out.println("wrote " + mByteCount + " bytes");
                }
            }
            jos.flush();
            jos.closeEntry();
        } catch (IOException ioe) {
            throw ioe;
        } finally {
            fis.close();
        }
    }
}

From source file:com.streamsets.pipeline.maven.rbgen.RBGenMojo.java

private void addFile(String path, File file, JarOutputStream jar) throws IOException {
    try (BufferedInputStream in = new BufferedInputStream(new FileInputStream(file))) {
        JarEntry entry = new JarEntry(path);
        entry.setTime(file.lastModified());
        jar.putNextEntry(entry);/*  w  ww.j a  va  2 s  .  com*/
        IOUtils.copy(in, jar);
        jar.closeEntry();
    }
}

From source file:averroes.JarFile.java

/**
 * Add the file read from the source input stream with the given entry name
 * to this JAR file./*w  w w . j a  v  a2s .  com*/
 * 
 * @param source
 * @param entryName
 * @throws IOException
 */
public void add(InputStream source, String entryName) throws IOException {
    JarEntry entry = new JarEntry(entryName);
    entry.setTime(System.currentTimeMillis());
    getJarOutputStream().putNextEntry(entry);

    byte[] buffer = new byte[1024];
    int len;
    while ((len = source.read(buffer)) != -1) {
        getJarOutputStream().write(buffer, 0, len);
    }
    getJarOutputStream().closeEntry();
    source.close();
}

From source file:averroes.JarFile.java

/**
 * Add the given file with the given entry name to this JAR file.
 * /*from w w w.j  a  v a2 s.  c om*/
 * @param source
 * @param entryName
 * @throws IOException
 */
public void add(File source, String entryName) throws IOException {
    JarEntry entry = new JarEntry(entryName);
    entry.setTime(source.lastModified());
    getJarOutputStream().putNextEntry(entry);
    BufferedInputStream in = new BufferedInputStream(new FileInputStream(source));

    byte[] buffer = new byte[1024];
    int len;
    while ((len = in.read(buffer)) != -1) {
        getJarOutputStream().write(buffer, 0, len);
    }
    getJarOutputStream().closeEntry();
    in.close();
}

From source file:averroes.JarFile.java

/**
 * Add a class file from source to the Jar file.
 * /* ww w  .ja v  a  2s. c o  m*/
 * @param dir
 * @param source
 * @throws IOException
 */
public void add(File dir, File source) throws IOException {
    BufferedInputStream in = null;
    try {
        if (source.isDirectory()) {
            String name = relativize(dir, source).replace("\\", "/");
            if (!name.isEmpty()) {
                if (!name.endsWith("/"))
                    name += "/";
                JarEntry entry = new JarEntry(name);
                entry.setTime(source.lastModified());
                getJarOutputStream().putNextEntry(entry);
                getJarOutputStream().closeEntry();
            }
            for (File nestedFile : source.listFiles())
                add(dir, nestedFile);
            return;
        }

        JarEntry entry = new JarEntry(relativize(dir, source).replace("\\", "/"));
        entry.setTime(source.lastModified());
        getJarOutputStream().putNextEntry(entry);
        in = new BufferedInputStream(new FileInputStream(source));

        byte[] buffer = new byte[1024];
        while (true) {
            int count = in.read(buffer);
            if (count == -1)
                break;
            getJarOutputStream().write(buffer, 0, count);
        }
        getJarOutputStream().closeEntry();
    } finally {
        if (in != null)
            in.close();
    }
}