Example usage for java.util.zip ZipEntry setTime

List of usage examples for java.util.zip ZipEntry setTime

Introduction

In this page you can find the example usage for java.util.zip ZipEntry setTime.

Prototype

public void setTime(long time) 

Source Link

Document

Sets the last modification time of the entry.

Usage

From source file:Main.java

public static void zipChildren(File folder, String prefix, ZipOutputStream out) throws IOException {
    File[] files = folder.listFiles();
    if (files == null)
        return;/*from   w  w w .  j  a  v  a  2  s  .  c  o  m*/
    for (File file : files) {
        if (file.isFile()) {
            String name = prefix + file.getName();
            ZipEntry entry = new ZipEntry(name);
            entry.setTime(file.lastModified());
            out.putNextEntry(entry);
            loadFromFile(file, out);
            out.closeEntry();
        } else if (file.isDirectory()) {
            zipChildren(file, prefix + file.getName() + "/", out);
        }
    }

}

From source file:Main.java

private static void compressDir(File srcFile, ZipOutputStream out, String destPath) throws IOException {
    if (srcFile.isDirectory()) {
        File subfile[] = srcFile.listFiles();
        for (int i = 0; i < subfile.length; i++) {
            compressDir(subfile[i], out, destPath);
        }//from   w w  w . ja v a 2 s  . c o  m
    } else {
        InputStream in = new FileInputStream(srcFile);
        String name = srcFile.getAbsolutePath().replace(destPath, "");
        if (name.startsWith("\\"))
            name = name.substring(1);
        ZipEntry entry = new ZipEntry(name);
        entry.setSize(srcFile.length());
        entry.setTime(srcFile.lastModified());
        out.putNextEntry(entry);
        int len = -1;
        byte buf[] = new byte[1024];
        while ((len = in.read(buf, 0, 1024)) != -1)
            out.write(buf, 0, len);

        out.closeEntry();
        in.close();
    }
}

From source file:Main.java

public static void compressAFileToZip(File zip, File source) throws IOException {
    Log.d("source: " + source.getAbsolutePath(), ", length: " + source.length());
    Log.d("zip:", zip.getAbsolutePath());
    zip.getParentFile().mkdirs();// www.  ja va 2s  .  c o m
    File tempFile = new File(zip.getAbsolutePath() + ".tmp");
    FileOutputStream fos = new FileOutputStream(tempFile);
    BufferedOutputStream bos = new BufferedOutputStream(fos);
    ZipOutputStream zos = new ZipOutputStream(bos);
    ZipEntry zipEntry = new ZipEntry(source.getName());
    zipEntry.setTime(source.lastModified());
    zos.putNextEntry(zipEntry);

    FileInputStream fis = new FileInputStream(source);
    BufferedInputStream bis = new BufferedInputStream(fis);
    byte[] bArr = new byte[4096];
    int byteRead = 0;
    while ((byteRead = bis.read(bArr)) != -1) {
        zos.write(bArr, 0, byteRead);
    }
    zos.flush();
    zos.closeEntry();
    zos.close();
    Log.d("zipEntry: " + zipEntry, "compressedSize: " + zipEntry.getCompressedSize());
    bos.flush();
    fos.flush();
    bos.close();
    fos.close();
    bis.close();
    fis.close();
    zip.delete();
    tempFile.renameTo(zip);
}

From source file:org.duracloud.common.util.IOUtil.java

/**
 * Adds the specified file to the zip output stream.
 *
 * @param file/*  w  w w .j  ava 2  s  .  com*/
 * @param zipOs
 */
public static void addFileToZipOutputStream(File file, ZipOutputStream zipOs) throws IOException {
    String fileName = file.getName();
    try (FileInputStream fos = new FileInputStream(file)) {
        ZipEntry zipEntry = new ZipEntry(fileName);
        zipEntry.setSize(file.length());
        zipEntry.setTime(System.currentTimeMillis());
        zipOs.putNextEntry(zipEntry);
        byte[] buf = new byte[1024];
        int bytesRead;
        while ((bytesRead = fos.read(buf)) > 0) {
            zipOs.write(buf, 0, bytesRead);
        }
        zipOs.closeEntry();
        fos.close();
    }
}

From source file:net.grinder.util.LogCompressUtils.java

/**
 * Compress multiple Files with the given encoding.
 *
 * @param logFiles     files to be compressed
 * @param fromEncoding log file encoding
 * @param toEncoding   compressed log file encoding
 * @return compressed file byte array//from   ww w  .  ja  v  a  2s .c om
 */
public static byte[] compress(File[] logFiles, Charset fromEncoding, Charset toEncoding) {
    FileInputStream fis = null;
    InputStreamReader isr = null;
    ByteArrayOutputStream out = null;
    ZipOutputStream zos = null;
    OutputStreamWriter osw = null;
    if (toEncoding == null) {
        toEncoding = Charset.defaultCharset();
    }
    if (fromEncoding == null) {
        fromEncoding = Charset.defaultCharset();
    }
    try {
        out = new ByteArrayOutputStream();
        zos = new ZipOutputStream(out);
        osw = new OutputStreamWriter(zos, toEncoding);
        for (File each : logFiles) {
            try {
                fis = new FileInputStream(each);
                isr = new InputStreamReader(fis, fromEncoding);
                ZipEntry zipEntry = new ZipEntry(each.getName());
                zipEntry.setTime(each.lastModified());
                zos.putNextEntry(zipEntry);
                char[] buffer = new char[COMPRESS_BUFFER_SIZE];
                int count;
                while ((count = isr.read(buffer, 0, COMPRESS_BUFFER_SIZE)) != -1) {
                    osw.write(buffer, 0, count);
                }
                osw.flush();
                zos.flush();
                zos.closeEntry();
            } catch (IOException e) {
                LOGGER.error("Error occurs while compressing {} : {}", each.getAbsolutePath(), e.getMessage());
                LOGGER.debug("Details ", e);
            } finally {
                IOUtils.closeQuietly(isr);
                IOUtils.closeQuietly(fis);
            }
        }
        zos.finish();
        zos.flush();
        return out.toByteArray();
    } catch (IOException e) {
        LOGGER.error("Error occurs while compressing log : {} ", e.getMessage());
        LOGGER.debug("Details : ", e);
        return null;
    } finally {
        IOUtils.closeQuietly(zos);
        IOUtils.closeQuietly(out);
        IOUtils.closeQuietly(osw);
    }
}

From source file:de.blizzy.backup.Utils.java

public static void zipFile(File source, File target) throws IOException {
    ZipOutputStream out = null;//from w  w w  .j  a v a  2s . c o m
    try {
        out = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(target)));
        out.setLevel(Deflater.BEST_COMPRESSION);

        ZipEntry entry = new ZipEntry(source.getName());
        entry.setTime(source.lastModified());
        out.putNextEntry(entry);

        Files.copy(source.toPath(), out);
    } finally {
        IOUtils.closeQuietly(out);
    }
}

From source file:org.fusesource.meshkeeper.util.internal.FileSupport.java

private static void recusiveJar(ZipOutputStream os, File source, String jarpath) throws IOException {
    String prefix = "";
    if (jarpath != null) {
        ZipEntry entry = new ZipEntry(jarpath);
        entry.setTime(source.lastModified() + ROUNDUP_MILLIS);
        os.putNextEntry(entry);//www .  j av  a  2 s  .  c o m
        prefix = jarpath + "/";
    }

    if (source.isDirectory()) {
        for (File file : source.listFiles()) {
            recusiveJar(os, file, prefix + file.getName());
        }
    } else {
        FileInputStream is = new FileInputStream(source);
        try {
            copy(is, os);
        } catch (IOException ioe) {
            IOException nioe = new IOException("Error jarring " + source);
            nioe.initCause(ioe);
            os.closeEntry();
            LOG.warn("Skipping jar entry for " + source.getAbsolutePath() + " - " + nioe.getMessage());
            if (LOG.isTraceEnabled()) {
                LOG.trace("Skipping jar entry for " + source.getAbsolutePath(), nioe);
            }
        } finally {
            close(is);
        }
    }
}

From source file:net.grinder.util.LogCompressUtil.java

/**
 * Compress the given file.//from ww  w.  j a  va  2s  .  c o  m
 * 
 * @param logFile
 *            file to be compressed
 * @return compressed file byte array
 */
public static byte[] compressFile(File logFile) {
    FileInputStream fio = null;
    ByteArrayOutputStream out = null;
    ZipOutputStream zos = null;
    try {
        fio = new FileInputStream(logFile);
        out = new ByteArrayOutputStream();
        zos = new ZipOutputStream(out);
        ZipEntry zipEntry = new ZipEntry("log.txt");
        zipEntry.setTime(new Date().getTime());
        zos.putNextEntry(zipEntry);
        byte[] buffer = new byte[COMPRESS_BUFFER_SIZE];
        int count = 0;
        while ((count = fio.read(buffer, 0, COMPRESS_BUFFER_SIZE)) != -1) {
            zos.write(buffer, 0, count);
        }
        zos.closeEntry();
        zos.finish();
        zos.flush();
        return out.toByteArray();
    } catch (IOException e) {
        LOGGER.error("Error occurs while compress {}", logFile.getAbsolutePath());
        LOGGER.error("Details", e);
        return null;
    } finally {
        IOUtils.closeQuietly(zos);
        IOUtils.closeQuietly(fio);
        IOUtils.closeQuietly(out);
    }
}

From source file:net.grinder.util.LogCompressUtil.java

/**
 * Compress multiple Files./*  w  w  w .j  a  va  2  s  .c o  m*/
 * 
 * @param logFiles
 *            files to be compressed
 * @return compressed file byte array
 */
public static byte[] compressFile(File[] logFiles) {
    FileInputStream fio = null;
    ByteArrayOutputStream out = null;
    ZipOutputStream zos = null;
    try {

        out = new ByteArrayOutputStream();
        zos = new ZipOutputStream(out);
        for (File each : logFiles) {
            try {
                fio = new FileInputStream(each);
                ZipEntry zipEntry = new ZipEntry(each.getName());
                zipEntry.setTime(each.lastModified());
                zos.putNextEntry(zipEntry);
                byte[] buffer = new byte[COMPRESS_BUFFER_SIZE];
                int count = 0;
                while ((count = fio.read(buffer, 0, COMPRESS_BUFFER_SIZE)) != -1) {
                    zos.write(buffer, 0, count);
                }
                zos.closeEntry();
            } catch (IOException e) {
                LOGGER.error("Error occurs while compress {}", each.getAbsolutePath());
                LOGGER.error("Details", e);
            } finally {
                IOUtils.closeQuietly(fio);
            }
        }
        zos.finish();
        zos.flush();
        return out.toByteArray();
    } catch (IOException e) {
        LOGGER.info("Error occurs while compress log : {} ", e.getMessage());
        LOGGER.debug("Details", e);
        return null;
    } finally {
        IOUtils.closeQuietly(zos);
        IOUtils.closeQuietly(fio);
        IOUtils.closeQuietly(out);
    }
}

From source file:org.openremote.modeler.utils.ZipUtils.java

/**
 * Compress.//from   ww  w. jav a2  s  .c o  m
 * 
 * @param outputFilePath the output file path
 * @param files the files
 * 
 * @return the file
 */
public static File compress(String outputFilePath, List<File> files) {
    final int buffer = 2048;
    BufferedInputStream bufferedInputStream;
    File outputFile = new File(outputFilePath);
    try {
        FileUtils.touch(outputFile);
    } catch (IOException e) {
        LOGGER.error("create zip file fail.", e);
        throw new FileOperationException("create zip file fail.", e);
    }
    FileOutputStream fileOutputStream = null;
    ZipOutputStream zipOutputStream = null;
    FileInputStream fileInputStream;
    try {
        fileOutputStream = new FileOutputStream(outputFilePath);
        zipOutputStream = new ZipOutputStream(new BufferedOutputStream(fileOutputStream));
        byte[] data = new byte[buffer];
        for (File file : files) {
            if (!file.exists()) {
                continue;
            }
            fileInputStream = new FileInputStream(file);
            bufferedInputStream = new BufferedInputStream(fileInputStream, buffer);
            ZipEntry entry = new ZipEntry(file.getName());
            entry.setSize(file.length());
            entry.setTime(file.lastModified());
            zipOutputStream.putNextEntry(entry);

            int count;
            while ((count = bufferedInputStream.read(data, 0, buffer)) != -1) {
                zipOutputStream.write(data, 0, count);
            }
            zipOutputStream.closeEntry();
            if (fileInputStream != null) {
                fileInputStream.close();
            }
            if (bufferedInputStream != null) {
                bufferedInputStream.close();
            }
        }
    } catch (FileNotFoundException e) {
        LOGGER.error("Can't find the output file.", e);
        throw new FileOperationException("Can't find the output file.", e);
    } catch (IOException e) {
        LOGGER.error("Can't compress file to zip archive, occured IOException", e);
        throw new FileOperationException("Can't compress file to zip archive, occured IOException", e);
    } finally {
        try {
            if (zipOutputStream != null) {
                zipOutputStream.close();
            }
            if (fileOutputStream != null) {
                fileOutputStream.close();
            }
        } catch (IOException e) {
            LOGGER.error("Close zipOutputStream and fileOutputStream occur IOException", e);
        }
    }
    return outputFile;
}