Example usage for java.util.zip ZipOutputStream putNextEntry

List of usage examples for java.util.zip ZipOutputStream putNextEntry

Introduction

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

Prototype

public void putNextEntry(ZipEntry e) throws IOException 

Source Link

Document

Begins writing a new ZIP file entry and positions the stream to the start of the entry data.

Usage

From source file:Main.java

static private void addToZip(String path, String srcFile, ZipOutputStream zip) throws IOException {
    File folder = new File(srcFile);
    if (folder.isDirectory()) {
        addFolderToZip(path, srcFile, zip, true);
    } else {/*  w w w.j  av a2s .co  m*/
        byte[] buf = new byte[1024];
        int len;
        FileInputStream in = null;
        try {
            in = new FileInputStream(srcFile);
            zip.putNextEntry(new ZipEntry(path + File.separator + folder.getName()));
            while ((len = in.read(buf)) > 0) {
                zip.write(buf, 0, len);
            }
        } finally {
            if (in != null)
                try {
                    in.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
        }
    }
}

From source file:com.datasalt.pangool.solr.TupleSolrOutputFormat.java

private static void createZip(File dir, File out) throws IOException {
    HashSet<File> files = new HashSet<File>();
    // take only conf/ and lib/
    for (String allowedDirectory : SolrRecordWriter.getAllowedConfigDirectories()) {
        File configDir = new File(dir, allowedDirectory);
        boolean configDirExists;
        /** If the directory does not exist, and is required, bail out */
        if (!(configDirExists = configDir.exists())
                && SolrRecordWriter.isRequiredConfigDirectory(allowedDirectory)) {
            throw new IOException(String.format("required configuration directory %s is not present in %s",
                    allowedDirectory, dir));
        }//  w  ww  .  ja va  2s.  com
        if (!configDirExists) {
            continue;
        }
        listFiles(configDir, files); // Store the files in the existing, allowed
                                     // directory configDir, in the list of files
                                     // to store in the zip file
    }

    out.delete();
    int subst = dir.toString().length();
    ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(out));
    byte[] buf = new byte[1024];
    for (File f : files) {
        ZipEntry ze = new ZipEntry(f.toString().substring(subst));
        zos.putNextEntry(ze);
        InputStream is = new FileInputStream(f);
        int cnt;
        while ((cnt = is.read(buf)) >= 0) {
            zos.write(buf, 0, cnt);
        }
        is.close();
        zos.flush();
        zos.closeEntry();
    }
    zos.close();
}

From source file:Main.java

final static private boolean createZipFile(String out, String... in) {
    InputStream is = null;/*  w ww.  j  a  v  a  2 s  .c o m*/
    ZipOutputStream zos = null;

    try {
        zos = new ZipOutputStream(new FileOutputStream(out));
    } catch (FileNotFoundException e2) {
        e2.printStackTrace();
    }
    try {
        for (int i = 0; i < in.length; i++) {
            is = new FileInputStream(in[i]);
            ZipEntry ze = new ZipEntry(in[i]);
            zos.putNextEntry(ze);
            int len = 0;
            byte[] buf = new byte[1024];
            while ((len = is.read(buf)) != -1) {
                zos.write(buf, 0, len);
            }
            is.close();
            zos.closeEntry();
        }
        zos.close();

    } catch (IOException e) {
        e.printStackTrace();
        return false;
    }
    return true;
}

From source file:Main.java

static void addDir(File dirObj, ZipOutputStream out) 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);//from   w w w. jav  a 2 s .c om
            continue;
        }
        FileInputStream in = new FileInputStream(files[i].getAbsolutePath());
        System.out.println(" Adding: " + files[i].getAbsolutePath());
        out.putNextEntry(new ZipEntry(files[i].getAbsolutePath()));
        int len;
        while ((len = in.read(tmpBuf)) > 0) {
            out.write(tmpBuf, 0, len);
        }
        out.closeEntry();
        in.close();
    }
}

From source file:com.chinamobile.bcbsp.fault.tools.Zip.java

/**
 * Compress files to *.zip./*from   ww  w .  j  av a  2 s.com*/
 * @param fileName
 *        file name to compress
 * @return compressed file.
 */
public static String compress(String fileName) {
    String targetFile = null;
    File sourceFile = new File(fileName);
    Vector<File> vector = getAllFiles(sourceFile);
    try {
        if (sourceFile.isDirectory()) {
            targetFile = fileName + ".zip";
        } else {
            char ch = '.';
            targetFile = fileName.substring(0, fileName.lastIndexOf(ch)) + ".zip";
        }
        BufferedInputStream bis = null;
        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(targetFile));
        ZipOutputStream zipos = new ZipOutputStream(bos);
        byte[] data = new byte[BUFFER];
        if (vector.size() > 0) {
            for (int i = 0; i < vector.size(); i++) {
                File file = vector.get(i);
                zipos.putNextEntry(new ZipEntry(getEntryName(fileName, file)));
                bis = new BufferedInputStream(new FileInputStream(file));
                int count;
                while ((count = bis.read(data, 0, BUFFER)) != -1) {
                    zipos.write(data, 0, count);
                }
                bis.close();
                zipos.closeEntry();
            }
            zipos.close();
            bos.close();
            return targetFile;
        } else {
            File zipNullfile = new File(targetFile);
            zipNullfile.getParentFile().mkdirs();
            zipNullfile.mkdir();
            return zipNullfile.getAbsolutePath();
        }
    } catch (IOException e) {
        LOG.error("[compress]", e);
        return "error";
    }
}

From source file:Utils.java

/**
 * Zip a list of file into one zip file.
 * //w  w  w.  j a v  a2 s  . co  m
 * @param files
 *          files to zip
 * @param targetZipFile
 *          target zip file
 * @throws IOException
 *           IO error exception can be thrown when copying ...
 */
public static void zipFile(final File[] files, final File targetZipFile) throws IOException {
    try {
        FileOutputStream fos = new FileOutputStream(targetZipFile);
        ZipOutputStream zos = new ZipOutputStream(fos);
        byte[] buffer = new byte[128];
        for (int i = 0; i < files.length; i++) {
            File currentFile = files[i];
            if (!currentFile.isDirectory()) {
                ZipEntry entry = new ZipEntry(currentFile.getName());
                FileInputStream fis = new FileInputStream(currentFile);
                zos.putNextEntry(entry);
                int read = 0;
                while ((read = fis.read(buffer)) != -1) {
                    zos.write(buffer, 0, read);
                }
                zos.closeEntry();
                fis.close();
            }
        }
        zos.close();
        fos.close();
    } catch (FileNotFoundException e) {
        System.out.println("File not found : " + e);
    }

}

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  ava  2 s  .c om*/

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

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

From source file:com.algomedica.service.LicenseManagerServiceImpl.java

public static byte[] zipBytes(String filename, byte[] input) throws IOException {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    ZipOutputStream zos = new ZipOutputStream(baos);
    ZipEntry entry = new ZipEntry(filename);
    entry.setSize(input.length);//  w  w w. j a va  2s  .  co  m
    zos.putNextEntry(entry);
    zos.write(input);
    zos.closeEntry();
    zos.close();
    return baos.toByteArray();
}

From source file:com.amalto.core.jobox.util.JoboxUtil.java

private static void zip(File file, String zipPath, ZipOutputStream zos) throws IOException {
    FileInputStream is = null;//from w w  w . j av  a2 s. co  m
    try {
        byte[] buf = new byte[1024];
        // Add ZIP entry to output stream.
        zos.putNextEntry(new ZipEntry(zipPath));
        // Transfer bytes from the file to the ZIP file
        int len;
        is = new FileInputStream(file);
        while ((len = is.read(buf)) > 0) {
            zos.write(buf, 0, len);
        }
    } finally {
        IOUtils.closeQuietly(is);
    }
}

From source file:Main.java

/**
 * Compress files to a zip/*from   w  w  w .j ava  2s .c o  m*/
 * @param zip
 * @param files
 * @throws IOException 
 */
public static void compressToZip(File zip, File[] files) throws IOException {
    byte data[] = new byte[BUFFER];
    FileOutputStream fozip = new FileOutputStream(zip);
    ZipOutputStream zo = new ZipOutputStream(new BufferedOutputStream(fozip));
    for (int i = 0; i < files.length; i++) {
        System.out.println("Adding:" + files[i]);
        FileInputStream fi = new FileInputStream(files[i]);
        BufferedInputStream origin = new BufferedInputStream(fi, BUFFER);
        ZipEntry zipentry = new ZipEntry(files[i].getName());
        zo.putNextEntry(zipentry);
        int count;
        while ((count = origin.read(data, 0, BUFFER)) != -1) {
            zo.write(data, 0, count);
        }
        origin.close();
    }
    zo.close();
}