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:ezbake.frack.submitter.util.JarUtil.java

private static void add(File source, final String prefix, JarOutputStream target) throws IOException {
    BufferedInputStream in = null;
    log.debug("Adding file {} to jar", source.getName());
    try {//  w  ww.j a  v  a 2  s  .co  m
        String entryPath = source.getPath().replace("\\", "/").replace(prefix, "");
        if (entryPath.startsWith("/")) {
            entryPath = entryPath.substring(1);
        }
        if (source.isDirectory()) {
            if (!entryPath.isEmpty()) {
                if (!entryPath.endsWith("/")) {
                    entryPath += "/";
                }
                JarEntry entry = new JarEntry(entryPath);
                entry.setTime(source.lastModified());
                target.putNextEntry(entry);
                target.closeEntry();
            }
            for (File nestedFile : source.listFiles()) {
                add(nestedFile, prefix, target);
            }
        } else {
            JarEntry entry = new JarEntry(entryPath);
            entry.setTime(source.lastModified());
            target.putNextEntry(entry);
            in = new BufferedInputStream(new FileInputStream(source));

            byte[] buffer = new byte[BUFFER_SIZE];
            int len;
            while ((len = in.read(buffer)) > 0) {
                target.write(buffer, 0, len);
            }
            target.closeEntry();
        }
    } finally {
        if (in != null) {
            in.close();
        }
    }
}

From source file:net.adamcin.oakpal.testing.TestPackageUtil.java

private static void add(final File root, final File source, final JarOutputStream target) throws IOException {
    if (root == null || source == null) {
        throw new IllegalArgumentException("Cannot add from a null file");
    }// w  w w . j a  v a 2 s  . c o m
    if (!(source.getPath() + "/").startsWith(root.getPath() + "/")) {
        throw new IllegalArgumentException("source must be the same file or a child of root");
    }
    final String relPath;
    if (!root.getPath().equals(source.getPath())) {
        relPath = source.getPath().substring(root.getPath().length() + 1).replace(File.separator, "/");
    } else {
        relPath = "";
    }
    if (source.isDirectory()) {
        if (!relPath.isEmpty()) {
            String name = relPath;
            if (!name.endsWith("/")) {
                name += "/";
            }
            JarEntry entry = new JarEntry(name);
            entry.setTime(source.lastModified());
            target.putNextEntry(entry);
            target.closeEntry();
        }
        File[] children = source.listFiles();
        if (children != null) {
            for (File nestedFile : children) {
                add(root, nestedFile, target);
            }
        }
    } else {
        JarEntry entry = new JarEntry(relPath);
        entry.setTime(source.lastModified());
        target.putNextEntry(entry);
        try (InputStream in = new BufferedInputStream(new FileInputStream(source))) {
            byte[] buffer = new byte[1024];
            while (true) {
                int count = in.read(buffer);
                if (count == -1)
                    break;
                target.write(buffer, 0, count);
            }
            target.closeEntry();
        }
    }
}

From source file:ai.h2o.servicebuilder.Util.java

/**
 * Create jar archive out of files list. Names in archive have paths starting from relativeToDir
 *
 * @param tobeJared     list of files//from w  w w. j a  v  a2s. c  om
 * @param relativeToDir starting directory for paths
 * @return jar as byte array
 * @throws IOException
 */
public static byte[] createJarArchiveByteArray(File[] tobeJared, String relativeToDir) throws IOException {
    int BUFFER_SIZE = 10240;
    byte buffer[] = new byte[BUFFER_SIZE];
    ByteArrayOutputStream stream = new ByteArrayOutputStream();
    JarOutputStream out = new JarOutputStream(stream, new Manifest());

    for (File t : tobeJared) {
        if (t == null || !t.exists() || t.isDirectory()) {
            if (t != null && !t.isDirectory())
                logger.error("Can't add to jar {}", t);
            continue;
        }
        // Create jar entry
        String filename = t.getPath().replace(relativeToDir, "").replace("\\", "/");
        //      if (filename.endsWith("MANIFEST.MF")) { // skip to avoid duplicates
        //        continue;
        //      }
        JarEntry jarAdd = new JarEntry(filename);
        jarAdd.setTime(t.lastModified());
        out.putNextEntry(jarAdd);

        // Write file to archive
        FileInputStream in = new FileInputStream(t);
        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();
    return stream.toByteArray();
}

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

private static void add(File source, JarOutputStream target) throws IOException {
    BufferedInputStream in = null;
    try {//from w  ww .  ja  va  2  s  .c  o m
        if (source.isDirectory()) {
            String name = source.getPath().replace("\\", "/");
            if (!name.isEmpty()) {
                if (!name.endsWith("/"))
                    name += "/";
                JarEntry entry = new JarEntry(name);
                entry.setTime(source.lastModified());
                target.putNextEntry(entry);
                target.closeEntry();
            }
            for (File nestedFile : source.listFiles())
                add(nestedFile, target);
            return;
        }

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

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

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

public static void createJarArchive(File jarFile, File[] listFiles) throws IOException {
    byte b[] = new byte[10240];
    FileOutputStream fout = new FileOutputStream(jarFile);
    JarOutputStream out = new JarOutputStream(fout, new Manifest());
    for (int i = 0; i < listFiles.length; i++) {
        if (listFiles[i] == null || !listFiles[i].exists() || listFiles[i].isDirectory()) {
            System.out.println();
        }//from ww  w.  j  av  a2  s .com
        JarEntry addFiles = new JarEntry(listFiles[i].getName());
        addFiles.setTime(listFiles[i].lastModified());
        out.putNextEntry(addFiles);

        FileInputStream fin = new FileInputStream(listFiles[i]);
        while (true) {
            int len = fin.read(b, 0, b.length);
            if (len <= 0)
                break;
            out.write(b, 0, len);
        }
        fin.close();
    }
    out.close();
    fout.close();
}

From source file:com.jhash.oimadmin.Utils.java

public static void createJarFileFromContent(Map<String, byte[]> content, String[] fileSequence,
        String jarFileName) {/*w w  w .  jav  a  2s  .c  o m*/
    logger.trace("createJarFileFromContent({},{})", content, jarFileName);
    logger.trace("Trying to create a new jar file");
    try (ZipOutputStream jarFileOutputStream = new ZipOutputStream(new FileOutputStream(jarFileName))) {
        jarFileOutputStream.setMethod(ZipOutputStream.STORED);
        for (String jarItem : fileSequence) {
            logger.trace("Processing item {}", jarItem);
            byte[] fileContent = content.get(jarItem);
            if (fileContent == null)
                throw new NullPointerException("Failed to locate content for file " + jarItem);
            JarEntry pluginXMLFileEntry = new JarEntry(jarItem);
            pluginXMLFileEntry.setTime(System.currentTimeMillis());
            pluginXMLFileEntry.setSize(fileContent.length);
            pluginXMLFileEntry.setCompressedSize(fileContent.length);
            CRC32 crc = new CRC32();
            crc.update(fileContent);
            pluginXMLFileEntry.setCrc(crc.getValue());
            jarFileOutputStream.putNextEntry(pluginXMLFileEntry);
            jarFileOutputStream.write(fileContent);
            jarFileOutputStream.closeEntry();
        }
    } catch (Exception exception) {
        throw new OIMAdminException("Failed to create the Jar file " + jarFileName, exception);
    }
}

From source file:com.jhash.oimadmin.Utils.java

public static void createJarFileFromDirectory(String directory, String jarFileName) {
    File jarDirectory = new File(directory);
    try (JarOutputStream jarFileOutputStream = new JarOutputStream(new FileOutputStream(jarFileName))) {
        for (Iterator<File> fileIterator = FileUtils.iterateFiles(jarDirectory, null, true); fileIterator
                .hasNext();) {/*from w ww . ja  va2 s. com*/
            File inputFile = fileIterator.next();
            String inputFileName = inputFile.getAbsolutePath();
            String directoryFileName = jarDirectory.getAbsolutePath();
            String relativeInputFileName = inputFileName.substring(directoryFileName.length() + 1);
            JarEntry newFileEntry = new JarEntry(relativeInputFileName);
            newFileEntry.setTime(System.currentTimeMillis());
            jarFileOutputStream.putNextEntry(newFileEntry);
            jarFileOutputStream.write(FileUtils.readFileToByteArray(inputFile));
        }
    } catch (Exception exception) {
        throw new OIMAdminException(
                "Failed to create the jar file " + jarFileName + " from directory " + directory, exception);
    }
}

From source file:oz.hadoop.yarn.api.utils.JarUtils.java

/**
 *
 * @param source//  w ww .  j  ava 2s.  c  o m
 * @param lengthOfOriginalPath
 * @param target
 * @throws IOException
 */
private static void add(File source, int lengthOfOriginalPath, JarOutputStream target) throws IOException {
    BufferedInputStream in = null;
    try {
        String path = source.getAbsolutePath();
        path = path.substring(lengthOfOriginalPath);

        if (source.isDirectory()) {
            String name = path.replace("\\", "/");
            if (!name.isEmpty()) {
                if (!name.endsWith("/")) {
                    name += "/";
                }
                JarEntry entry = new JarEntry(name.substring(1)); // avoiding absolute path warning
                target.putNextEntry(entry);
                target.closeEntry();
            }

            for (File nestedFile : source.listFiles()) {
                add(nestedFile, lengthOfOriginalPath, target);
            }

            return;
        }

        JarEntry entry = new JarEntry(path.replace("\\", "/").substring(1)); // avoiding absolute path warning
        entry.setTime(source.lastModified());
        try {
            target.putNextEntry(entry);
            in = new BufferedInputStream(new FileInputStream(source));

            byte[] buffer = new byte[1024];
            while (true) {
                int count = in.read(buffer);
                if (count == -1) {
                    break;
                }
                target.write(buffer, 0, count);
            }
            target.closeEntry();
        } catch (Exception e) {
            String message = e.getMessage();
            if (StringUtils.hasText(message)) {
                if (!message.toLowerCase().contains("duplicate")) {
                    throw new IllegalStateException(e);
                }
                logger.warn(message);
            } else {
                throw new IllegalStateException(e);
            }
        }
    } finally {
        if (in != null)
            in.close();
    }
}

From source file:io.dstream.tez.utils.ClassPathUtils.java

/**
 *
 * @param source//from   w  w  w .ja  va2 s . c  o m
 * @param lengthOfOriginalPath
 * @param target
 * @throws IOException
 */
private static void add(File source, int lengthOfOriginalPath, JarOutputStream target) throws IOException {
    BufferedInputStream in = null;
    try {
        String path = source.getAbsolutePath();
        path = path.substring(lengthOfOriginalPath);

        if (source.isDirectory()) {
            String name = path.replace("\\", "/");
            if (!name.isEmpty()) {
                if (!name.endsWith("/")) {
                    name += "/";
                }
                JarEntry entry = new JarEntry(name.substring(1)); // avoiding absolute path warning
                target.putNextEntry(entry);
                target.closeEntry();
            }

            for (File nestedFile : source.listFiles()) {
                add(nestedFile, lengthOfOriginalPath, target);
            }

            return;
        }

        JarEntry entry = new JarEntry(path.replace("\\", "/").substring(1)); // avoiding absolute path warning
        entry.setTime(source.lastModified());
        try {
            target.putNextEntry(entry);
            in = new BufferedInputStream(new FileInputStream(source));

            byte[] buffer = new byte[1024];
            while (true) {
                int count = in.read(buffer);
                if (count == -1) {
                    break;
                }
                target.write(buffer, 0, count);
            }
            target.closeEntry();
        } catch (Exception e) {
            String message = e.getMessage();
            if (message != null) {
                if (!message.toLowerCase().contains("duplicate")) {
                    throw new IllegalStateException(e);
                }
                logger.warn(message);
            } else {
                throw new IllegalStateException(e);
            }
        }
    } finally {
        if (in != null)
            in.close();
    }
}

From source file:de.fhg.igd.mapviewer.server.file.FileTiler.java

/**
 * Creates a Jar archive that includes the given list of files
 * /*  w w  w  .  j av  a2s  .c  om*/
 * @param archiveFile the name of the jar archive file
 * @param tobeJared the files to be included in the jar file
 * 
 * @return if the operation was successful
 */
public static boolean createJarArchive(File archiveFile, List<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.size(); i++) {
            if (tobeJared.get(i) == null || !tobeJared.get(i).exists() || tobeJared.get(i).isDirectory())
                continue; // Just in case...
            log.debug("Adding " + tobeJared.get(i).getName());

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

            // Write file to archive
            FileInputStream in = new FileInputStream(tobeJared.get(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 completed OK");
        return true;
    } catch (Exception e) {
        log.error("Creating jar file failed", e);
        return false;
    }
}