Example usage for java.util.zip ZipEntry ZipEntry

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

Introduction

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

Prototype

public ZipEntry(ZipEntry e) 

Source Link

Document

Creates a new zip entry with fields taken from the specified zip entry.

Usage

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

/**
 * Compress the given file.// w w w  .  j  a  va2  s  . 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:org.eclipse.gemini.blueprint.test.internal.util.jar.JarUtils.java

/**
 * /*from  w  ww. j av a  2s . co  m*/
 * Writes a resource content to a jar.
 * 
 * @param res
 * @param entryName
 * @param jarStream
 * @param bufferSize
 * @return the number of bytes written to the jar file
 * @throws Exception
 */
public static int writeToJar(Resource res, String entryName, JarOutputStream jarStream, int bufferSize)
        throws IOException {
    byte[] readWriteJarBuffer = new byte[bufferSize];

    // remove leading / if present.
    if (entryName.charAt(0) == '/')
        entryName = entryName.substring(1);

    jarStream.putNextEntry(new ZipEntry(entryName));
    InputStream entryStream = res.getInputStream();

    int numberOfBytes;

    // read data into the buffer which is later on written to the jar.
    while ((numberOfBytes = entryStream.read(readWriteJarBuffer)) != -1) {
        jarStream.write(readWriteJarBuffer, 0, numberOfBytes);
    }
    return numberOfBytes;
}

From source file:io.apigee.buildTools.enterprise4g.utils.ZipUtils.java

static void addDir(File dirObj, ZipOutputStream out, File root, String prefix) throws IOException {
    File[] files = dirObj.listFiles();
    byte[] tmpBuf = new byte[1024];

    for (int i = 0; i < files.length; i++) {
        if (files[i].isDirectory()) {
            addDir(files[i], out, root, prefix);
            continue;
        }// w w  w.  j a va 2 s  .c o m
        FileInputStream in = new FileInputStream(files[i].getAbsolutePath());
        log.debug(" Adding: " + files[i].getAbsolutePath());

        String relativePath = files[i].getCanonicalPath().substring(root.getCanonicalPath().length());
        while (relativePath.startsWith("/")) {
            relativePath = relativePath.substring(1);
        }
        while (relativePath.startsWith("\\")) {
            relativePath = relativePath.substring(1);
        }

        String left = Matcher.quoteReplacement("\\");
        String right = Matcher.quoteReplacement("/");

        relativePath = relativePath.replaceAll(left, right);
        relativePath = (prefix == null) ? relativePath : prefix + "/" + relativePath;
        out.putNextEntry(new ZipEntry(relativePath));
        int len;
        while ((len = in.read(tmpBuf)) > 0) {
            out.write(tmpBuf, 0, len);
        }
        out.closeEntry();
        in.close();
    }
}

From source file:mpimp.assemblxweb.util.J5FileUtils.java

private static void zipDirectoryRecursively(String dirPath, BufferedInputStream origin, int BUFFER,
        ZipOutputStream out, byte[] data, String parentDirName) throws Exception {
    File directory = new File(dirPath);
    File content[] = directory.listFiles();

    for (int i = 0; i < content.length; i++) {
        if (content[i].isDirectory()) {
            String parentPath = parentDirName + File.separator + content[i].getName();
            zipDirectoryRecursively(content[i].getAbsolutePath(), origin, BUFFER, out, data, parentPath);
        } else {// www. ja v a2s.  c  o m
            String filePathInDirectory = parentDirName + File.separator + content[i].getName();
            FileInputStream in = new FileInputStream(content[i].getAbsolutePath());
            origin = new BufferedInputStream(in, BUFFER);
            ZipEntry zipEntry = new ZipEntry(filePathInDirectory);
            out.putNextEntry(zipEntry);
            int count;
            while ((count = origin.read(data, 0, BUFFER)) != -1) {
                out.write(data, 0, count);
            }
            origin.close();
            in.close();
        }
    }
}

From source file:com.nuvolect.securesuite.util.OmniZip.java

public static void zipFile(String basePath, OmniFile file, ZipOutputStream zout) throws IOException {

    LogUtil.log(LogUtil.LogType.OMNI_ZIP, "zipFile : " + file.getPath());

    byte[] buffer = new byte[4096];

    InputStream fin = file.getFileInputStream();

    zout.putNextEntry(new ZipEntry(basePath + file.getName()));

    int length;//from   w  w w.  j  a  v a  2 s .  c  o  m

    while ((length = fin.read(buffer)) > 0) {
        zout.write(buffer, 0, length);
    }

    zout.closeEntry();
    fin.close();
}

From source file:com.facebook.buck.util.zip.ZipScrubberTest.java

@Test
public void modificationTimesExceedShort() throws Exception {
    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    byte[] data = "data1".getBytes(Charsets.UTF_8);
    try (ZipOutputStream out = new ZipOutputStream(byteArrayOutputStream)) {
        for (long i = 0; i < Short.MAX_VALUE + 1; i++) {
            ZipEntry entry = new ZipEntry("file" + i);
            entry.setSize(data.length);//  w w w . ja  va2s  .  co m
            out.putNextEntry(entry);
            out.write(data);
            out.closeEntry();
        }
    }

    byte[] bytes = byteArrayOutputStream.toByteArray();
    ZipScrubber.scrubZipBuffer(bytes.length, ByteBuffer.wrap(bytes));

    // Iterate over each of the entries, expecting to see all zeros in the time fields.
    Date dosEpoch = new Date(ZipUtil.dosToJavaTime(ZipConstants.DOS_FAKE_TIME));
    try (ZipInputStream is = new ZipInputStream(new ByteArrayInputStream(bytes))) {
        for (ZipEntry entry = is.getNextEntry(); entry != null; entry = is.getNextEntry()) {
            assertThat(entry.getName(), new Date(entry.getTime()), Matchers.equalTo(dosEpoch));
        }
    }
}

From source file:cross.io.misc.WorkflowZipper.java

private void addZipEntry(final int bufsize, final ZipOutputStream zos, final byte[] input_buffer,
        final File file, final HashSet<String> zipEntries) throws IOException {
    log.debug("Adding zip entry for file {}", file);
    if (file.exists() && file.isFile()) {
        // Use the file name for the ZipEntry name.
        final ZipEntry zip_entry = new ZipEntry(file.getName());
        if (zipEntries.contains(file.getName())) {
            log.info("Skipping duplicate zip entry {}", file.getName());
            return;
        } else {/*from  w w w.ja  va  2 s.  co  m*/
            zipEntries.add(file.getName());
        }
        zos.putNextEntry(zip_entry);

        // Create a buffered input stream from the file stream.
        final FileInputStream in = new FileInputStream(file);
        // Read from source into buffer and write, thereby compressing
        // on the fly
        try (BufferedInputStream source = new BufferedInputStream(in, bufsize)) {
            // Read from source into buffer and write, thereby compressing
            // on the fly
            int len = 0;
            while ((len = source.read(input_buffer, 0, bufsize)) != -1) {
                zos.write(input_buffer, 0, len);
            }
            zos.flush();
        }
        zos.closeEntry();
    } else {
        log.warn("Skipping nonexistant file or directory {}", file);
    }
}

From source file:apim.restful.exportimport.utils.ArchiveGenerator.java

public static void addToZip(File directoryToZip, File file, ZipOutputStream zos)
        throws FileNotFoundException, IOException {

    FileInputStream fis = new FileInputStream(file);

    // we want the zipEntry's path to be a relative path that is relative
    // to the directory being zipped, so chop off the rest of the path
    String zipFilePath = file.getCanonicalPath().substring(directoryToZip.getCanonicalPath().length() + 1,
            file.getCanonicalPath().length());
    System.out.println("Writing '" + zipFilePath + "' to zip file");
    ZipEntry zipEntry = new ZipEntry(zipFilePath);
    zos.putNextEntry(zipEntry);/*  ww  w  .j  av a 2  s .co  m*/

    byte[] bytes = new byte[1024];
    int length;
    while ((length = fis.read(bytes)) >= 0) {
        zos.write(bytes, 0, length);
    }

    zos.closeEntry();
    fis.close();
}

From source file:JarMaker.java

/**
 * used by downloadAndPack//from  ww w  .j  a va  2  s  .  c  o  m
 * @param _jout
 * @param _dir
 * @param _prefix
 * @throws IOException
 */
public static void add(JarOutputStream _jout, File _dir, String _prefix) throws IOException {

    File[] content = _dir.listFiles();
    if (_dir.isDirectory()) {
        for (int i = 0, l = content.length; i < l; ++i) {
            if (content[i].isDirectory()) {
                _jout.putNextEntry(
                        new ZipEntry(_prefix + (_prefix.equals("") ? "" : "/") + content[i].getName() + "/"));
                add(_jout, content[i], _prefix + (_prefix.equals("") ? "" : "/") + content[i].getName());
            } else {
                _jout.putNextEntry(
                        new ZipEntry(_prefix + (_prefix.equals("") ? "" : "/") + content[i].getName()));
                FileInputStream in = new FileInputStream(content[i]);
                write(in, _jout);
                in.close();
            }
        }
    } else {
        _jout.putNextEntry(new ZipEntry(_prefix + (_prefix.equals("") ? "" : "/") + _dir.getName()));
        FileInputStream in = new FileInputStream(_dir);
        write(in, _jout);
        in.close();
    }
}

From source file:org.cloudfoundry.tools.io.zip.ZipArchiveTest.java

@Before
public void setup() throws Exception {
    MockitoAnnotations.initMocks(this);
    this.zipFile = this.temporaryFolder.newFile("zipfile.zip");
    ZipOutputStream zipOutputStream = new ZipOutputStream(new FileOutputStream(this.zipFile));
    try {/*from  w w w.j ava  2 s. com*/
        zipOutputStream.putNextEntry(new ZipEntry("/a/b/c.txt"));
        zipOutputStream.write("c".getBytes());
        zipOutputStream.putNextEntry(new ZipEntry("/d/"));
        zipOutputStream.putNextEntry(new ZipEntry("/d/e/"));
        zipOutputStream.putNextEntry(new ZipEntry("/d/f/"));
        zipOutputStream.putNextEntry(new ZipEntry("/d/f/g.txt"));
        zipOutputStream.putNextEntry(new ZipEntry("/d/f/h/"));
        zipOutputStream.write("g".getBytes());
    } finally {
        zipOutputStream.close();
    }
    this.zip = new ZipArchive(new LocalFolder(this.zipFile.getParentFile()).getFile(this.zipFile.getName()));
}