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:com.ibm.watson.developer_cloud.retrieve_and_rank.v1.util.ZipUtils.java

private static void writeZipEntry(ZipOutputStream out, String name, byte[] data) throws IOException {
    final ZipEntry entry = new ZipEntry(StringUtils.removeStart(name, "/"));
    out.putNextEntry(entry);// ww w .  j a  v a2 s.co m
    out.write(data, 0, data.length);
    out.closeEntry();
}

From source file:fr.univrouen.poste.services.ZipService.java

public void writeZip(List<PosteCandidature> posteCandidatures, OutputStream destStream)
        throws IOException, SQLException {

    ZipOutputStream out = new ZipOutputStream(destStream);

    for (PosteCandidature posteCandidature : posteCandidatures) {
        String folderName = posteCandidature.getPoste().getNumEmploi().concat("/");
        folderName = folderName.concat(posteCandidature.getCandidat().getNom().concat("-"));
        folderName = folderName.concat(posteCandidature.getCandidat().getPrenom().concat("-"));
        folderName = folderName.concat(posteCandidature.getCandidat().getNumCandidat().concat("/"));
        for (PosteCandidatureFile posteCandidatureFile : posteCandidature.getCandidatureFiles()) {
            String fileName = posteCandidatureFile.getId().toString().concat("-")
                    .concat(posteCandidatureFile.getFilename());
            String folderFileName = folderName.concat(fileName);
            out.putNextEntry(new ZipEntry(folderFileName));
            InputStream inputStream = posteCandidatureFile.getBigFile().getBinaryFile().getBinaryStream();
            BufferedInputStream bufInputStream = new BufferedInputStream(inputStream, BUFFER);
            byte[] bytes = new byte[BUFFER];
            int length;
            while ((length = bufInputStream.read(bytes)) >= 0) {
                out.write(bytes, 0, length);
            }//from  w w  w  .ja v  a 2  s  .c  o m
            out.closeEntry();
        }
    }
    out.close();
}

From source file:com.matze5800.paupdater.Functions.java

public static void addFilesToExistingZip(File zipFile, File[] files) throws IOException {
    File tempFile = new File(Environment.getExternalStorageDirectory() + "/pa_updater", "temp_kernel.zip");
    tempFile.delete();/*from w  ww .j  a v  a2  s . c om*/

    boolean renameOk = zipFile.renameTo(tempFile);
    if (!renameOk) {
        throw new RuntimeException(
                "could not rename the file " + zipFile.getAbsolutePath() + " to " + tempFile.getAbsolutePath());
    }
    byte[] buf = new byte[1024];

    ZipInputStream zin = new ZipInputStream(new FileInputStream(tempFile));
    ZipOutputStream out = new ZipOutputStream(new FileOutputStream(zipFile));

    ZipEntry entry = zin.getNextEntry();
    while (entry != null) {
        String name = entry.getName();
        boolean notInFiles = true;
        for (File f : files) {
            if (f.getName().equals(name)) {
                notInFiles = false;
                break;
            }
        }
        if (notInFiles) {

            out.putNextEntry(new ZipEntry(name));

            int len;
            while ((len = zin.read(buf)) > 0) {
                out.write(buf, 0, len);
            }
        }
        entry = zin.getNextEntry();
    }
    zin.close();

    for (int i = 0; i < files.length; i++) {
        InputStream in = new FileInputStream(files[i]);
        out.putNextEntry(new ZipEntry(files[i].getName()));

        int len;
        while ((len = in.read(buf)) > 0) {
            out.write(buf, 0, len);
        }
        out.closeEntry();
        in.close();
    }
    out.close();
    tempFile.delete();
}

From source file:gov.nih.nci.caarray.services.external.v1_0.data.JavaDataApiUtils.java

/**
 * {@inheritDoc}/*from   ww  w.j  a v a  2  s .com*/
 */
public void copyFileContentsZipToOutputStream(Iterable<CaArrayEntityReference> fileRefs, OutputStream ostream)
        throws InvalidReferenceException, DataTransferException, IOException {
    ZipOutputStream zos = new ZipOutputStream(ostream);
    for (CaArrayEntityReference fileRef : fileRefs) {
        FileStreamableContents fsContents = dataService.streamFileContents(fileRef, true);
        zos.putNextEntry(new ZipEntry(fsContents.getMetadata().getName()));
        readFully(fsContents.getContentStream(), zos, true);
    }
    zos.finish();
}

From source file:com.l2jfree.gameserver.util.DatabaseBackupManager.java

public static void makeBackup() {
    File f = new File(Config.DATAPACK_ROOT, Config.DATABASE_BACKUP_SAVE_PATH);
    if (!f.mkdirs() && !f.exists()) {
        _log.warn("Could not create folder " + f.getAbsolutePath());
        return;/*from  www  . j  a va2  s  .  c  o m*/
    }

    _log.info("DatabaseBackupManager: backing up `" + Config.DATABASE_BACKUP_DATABASE_NAME + "`...");

    Process run = null;
    try {
        run = Runtime.getRuntime().exec("mysqldump" + " --user=" + Config.DATABASE_LOGIN + " --password="
                + Config.DATABASE_PASSWORD
                + " --compact --complete-insert --default-character-set=utf8 --extended-insert --lock-tables --quick --skip-triggers "
                + Config.DATABASE_BACKUP_DATABASE_NAME, null, new File(Config.DATABASE_BACKUP_MYSQLDUMP_PATH));
    } catch (Exception e) {
    } finally {
        if (run == null) {
            _log.warn("Could not execute mysqldump!");
            return;
        }
    }

    try {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd_HH-mm-ss");
        Date time = new Date();

        File bf = new File(f, sdf.format(time) + (Config.DATABASE_BACKUP_COMPRESSION ? ".zip" : ".sql"));
        if (!bf.createNewFile())
            throw new IOException("Cannot create backup file: " + bf.getCanonicalPath());
        InputStream input = run.getInputStream();
        OutputStream out = new FileOutputStream(bf);
        if (Config.DATABASE_BACKUP_COMPRESSION) {
            ZipOutputStream dflt = new ZipOutputStream(out);
            dflt.setMethod(ZipOutputStream.DEFLATED);
            dflt.setLevel(Deflater.BEST_COMPRESSION);
            dflt.setComment("L2JFree Schema Backup Utility\r\n\r\nBackup date: "
                    + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS z").format(time));
            dflt.putNextEntry(new ZipEntry(Config.DATABASE_BACKUP_DATABASE_NAME + ".sql"));
            out = dflt;
        }

        byte[] buf = new byte[4096];
        int written = 0;
        for (int read; (read = input.read(buf)) != -1;) {
            out.write(buf, 0, read);

            written += read;
        }
        input.close();
        out.close();

        if (written == 0) {
            bf.delete();
            BufferedReader br = new BufferedReader(new InputStreamReader(run.getErrorStream()));
            String line;
            while ((line = br.readLine()) != null)
                _log.warn("DatabaseBackupManager: " + line);
            br.close();
        } else
            _log.info("DatabaseBackupManager: Schema `" + Config.DATABASE_BACKUP_DATABASE_NAME
                    + "` backed up successfully in " + (System.currentTimeMillis() - time.getTime()) / 1000
                    + " s.");

        run.waitFor();
    } catch (Exception e) {
        _log.warn("DatabaseBackupManager: Could not make backup: ", e);
    }
}

From source file:com.dv.util.DataViewerZipUtil.java

/**
 * ZipOutputStream/*w  w  w  .ja va  2s  .c o m*/
 * @param srcFile 
 * @param zipOut ZipOutputStream?
 * @param ns ZIP
 * @throws IOException
 */
private static void doZipFile(File srcFile, ZipOutputStream zipOut, String ns) throws IOException {
    if (srcFile.isFile()) {
        zipOut.putNextEntry(new ZipEntry(ns + srcFile.getName()));
        InputStream is = FileUtils.openInputStream(srcFile);
        try {
            IOUtils.copy(is, zipOut);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            IOUtils.closeQuietly(is);
        }
        return;
    }
    for (File file : srcFile.listFiles()) {
        String entryName = ns + file.getName();

        if (file.isDirectory()) {
            entryName += File.separator;
            zipOut.putNextEntry(new ZipEntry(entryName));
        }
        doZipFile(file, zipOut, entryName);
    }
}

From source file:dk.netarkivet.common.utils.ZipUtils.java

/** Zip the contents of a directory into a file.
 *  Does *not* zip recursively.//from w ww.  j a v  a 2 s . c  o  m
 *
 * @param dir The directory to zip.
 * @param into The (zip) file to create.  The name should typically end
 * in .zip, but that is not required.
 */
public static void zipDirectory(File dir, File into) {
    ArgumentNotValid.checkNotNull(dir, "File dir");
    ArgumentNotValid.checkNotNull(into, "File into");
    ArgumentNotValid.checkTrue(dir.isDirectory(), "directory '" + dir + "' to zip is not a directory");
    ArgumentNotValid.checkTrue(into.getAbsoluteFile().getParentFile().canWrite(),
            "cannot write to '" + into + "'");

    File[] files = dir.listFiles();
    FileOutputStream out;
    try {
        out = new FileOutputStream(into);
    } catch (IOException e) {
        throw new IOFailure("Error creating ZIP outfile file '" + into + "'", e);
    }
    ZipOutputStream zipout = new ZipOutputStream(out);
    try {
        try {
            for (File f : files) {
                if (f.isFile()) {
                    ZipEntry entry = new ZipEntry(f.getName());
                    zipout.putNextEntry(entry);
                    FileUtils.writeFileToStream(f, zipout);
                } // Not doing directories yet.
            }
        } finally {
            zipout.close();
        }
    } catch (IOException e) {
        throw new IOFailure("Failed to zip directory '" + dir + "'", e);
    }
}

From source file:com.google.mr4c.sources.LogsDatasetSource.java

private void addZipEntry(ZipOutputStream zipStream, String name) throws IOException {
    ZipEntry entry = new ZipEntry(name);
    zipStream.putNextEntry(entry);/*from  ww w .j  av a2  s  . c  o m*/
    DataFileSource entrySource = m_fileSrc.getFileSource(name);
    InputStream input = entrySource.getFileInputStream();
    try {
        IOUtils.copy(input, zipStream);
        zipStream.closeEntry();
    } finally {
        input.close();
    }
}

From source file:com.opensymphony.xwork2.util.fs.JarEntryRevisionTest.java

private String createJarFile(long time) throws Exception {
    Manifest manifest = new Manifest();
    manifest.getMainAttributes().put(Attributes.Name.MANIFEST_VERSION, "1.0");
    Path jarPath = Paths/*w ww  .  j a  v  a 2 s  . c  o m*/
            .get(Thread.currentThread().getContextClassLoader().getResource("xwork-jar.jar").toURI())
            .getParent();
    File jarFile = jarPath.resolve("JarEntryRevisionTest_testNeedsReloading.jar").toFile();
    FileOutputStream fos = new FileOutputStream(jarFile, false);
    JarOutputStream target = new JarOutputStream(fos, manifest);
    target.putNextEntry(new ZipEntry("com/opensymphony/xwork2/util/fs/"));
    ZipEntry entry = new ZipEntry("com/opensymphony/xwork2/util/fs/JarEntryRevisionTest.class");
    entry.setTime(time);
    target.putNextEntry(entry);
    InputStream source = getClass()
            .getResourceAsStream("/com/opensymphony/xwork2/util/fs/JarEntryRevisionTest.class");
    IOUtils.copy(source, target);
    source.close();
    target.closeEntry();
    target.close();
    fos.close();

    return jarFile.toURI().toURL().toExternalForm();
}

From source file:com.clustercontrol.collect.util.ZipCompresser.java

/**
 * ?????1????//from  w ww .  ja  v a 2  s  .  com
 * 
 * @param inputFileNameList ??
 * @param outputFileName ?
 * 
 * @return ???
 */
protected static synchronized void archive(ArrayList<String> inputFileNameList, String outputFileName)
        throws HinemosUnknown {
    m_log.debug("archive() output file = " + outputFileName);

    byte[] buf = new byte[128];
    BufferedInputStream in = null;
    ZipEntry entry = null;
    ZipOutputStream out = null;

    // ?
    if (outputFileName == null || "".equals(outputFileName)) {
        HinemosUnknown e = new HinemosUnknown("archive output fileName is null ");
        m_log.info("archive() : " + e.getClass().getSimpleName() + ", " + e.getMessage());
        throw e;
    }
    File zipFile = new File(outputFileName);
    if (zipFile.exists()) {
        if (zipFile.isFile() && zipFile.canWrite()) {
            m_log.debug("archive() output file = " + outputFileName
                    + " is exists & file & writable. initialize file(delete)");
            if (!zipFile.delete())
                m_log.debug("Fail to delete " + zipFile.getAbsolutePath());
        } else {
            HinemosUnknown e = new HinemosUnknown("archive output fileName is directory or not writable ");
            m_log.info("archive() : " + e.getClass().getSimpleName() + ", " + e.getMessage());
            throw e;
        }
    }

    // ?
    try {
        // ????
        out = new ZipOutputStream(new FileOutputStream(outputFileName));

        for (String inputFileName : inputFileNameList) {
            m_log.debug("archive() input file name = " + inputFileName);

            // ????
            in = new BufferedInputStream(new FileInputStream(inputFileName));

            // ??
            String fileName = (new File(inputFileName)).getName();
            m_log.debug("archive() entry file name = " + fileName);
            entry = new ZipEntry(fileName);

            out.putNextEntry(entry);
            // ????
            int size;
            while ((size = in.read(buf, 0, buf.length)) != -1) {
                out.write(buf, 0, size);
            }
            // ??
            out.closeEntry();
            in.close();
        }
        // ? out.flush();
        out.close();

    } catch (IOException e) {
        m_log.warn("archive() archive error : " + outputFileName + e.getClass().getSimpleName() + ", "
                + e.getMessage(), e);
        throw new HinemosUnknown("archive error " + outputFileName, e);

    } finally {

        if (in != null) {
            try {
                in.close();
            } catch (IOException e) {
            }
        }

        if (out != null) {
            try {
                out.close();
            } catch (IOException e) {
            }
        }
    }
}