Example usage for java.util.zip ZipOutputStream closeEntry

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

Introduction

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

Prototype

public void closeEntry() throws IOException 

Source Link

Document

Closes the current ZIP entry and positions the stream for writing the next entry.

Usage

From source file:Main.java

final static private boolean createZipFile(String out, String... in) {
    InputStream is = null;//ww w. j  a v  a 2s . 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: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));
        }/*from w  w w.j  av a2s.  c  om*/
        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:com.gelakinetic.mtgfam.helpers.ZipUtils.java

/**
 * Zips all files specified in an ArrayList into a given file
 *
 * @param zipFile The file to zip files into
 * @param files   The files to be zipped
 * @param context The application context, for getting files and the like
 * @throws IOException IOException Thrown if something goes wrong with zipping and reading
 */// w  ww .  j a v  a  2  s . c om
private static void zipIt(File zipFile, ArrayList<File> files, Context context) throws IOException {

    byte[] buffer = new byte[1024];

    FileOutputStream fos = new FileOutputStream(zipFile);
    ZipOutputStream zos = new ZipOutputStream(fos);
    assert context.getFilesDir() != null;
    for (File file : files) {
        ZipEntry ze = new ZipEntry(file.getName());
        zos.putNextEntry(ze);

        FileInputStream in = new FileInputStream(file);

        int len;
        while ((len = in.read(buffer)) > 0) {
            zos.write(buffer, 0, len);
        }
        in.close();
    }

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

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

private static void zipSubDirectory(Context ctx, String basePath, OmniFile dir, ZipOutputStream zos)
        throws IOException {

    LogUtil.log(LogUtil.LogType.OMNI_ZIP, "zipSubDirectory : " + dir.getPath());

    OmniFile[] files = dir.listFiles();/*from   w  w w .  j  av a 2 s. c om*/

    for (OmniFile file : files) {

        if (file.isDirectory()) {
            String path = basePath + file.getName() + File.separator;
            zos.putNextEntry(new ZipEntry(path));
            zipSubDirectory(ctx, path, file, zos);
            zos.closeEntry();
        } else {
            if (file.isStd())// don't scan crypto volume
                MediaScannerConnection.scanFile(ctx, new String[] { file.getAbsolutePath() }, null, null);

            zipFile(basePath, file, zos);
        }
    }
}

From source file:nz.ac.otago.psyanlab.common.util.FileUtils.java

/**
 * Write the experiment definition to the zip output stream. This should be
 * done as the final operation in compressing the entire experiment as
 * earlier stages may modify this data that is to be written.
 * //from ww  w .  j a  va 2s . c o  m
 * @param out Output stream for encoding the data.
 * @param experiment Experiment definition.
 * @throws IOException
 */
private static void writeExperimentDefinitionToArchive(ZipOutputStream out, Experiment experiment)
        throws IOException {
    String paleDef = ModelUtils.getDataReaderWriter().toJson(experiment);
    InputStream in = new ByteArrayInputStream(paleDef.getBytes("UTF-16"));

    ZipEntry ze = new ZipEntry("experiment.json");
    out.putNextEntry(ze);

    try {
        copy(in, out);
        out.closeEntry();
    } finally {
        in.close();
    }
}

From source file:es.sm2.openppm.core.plugin.action.GenericAction.java

/**
 *
 * @param files/*  www .j a  v a2s  .  c o  m*/
 * @return
 * @throws IOException
 */
public static byte[] zipFiles(List<File> files) throws IOException {

    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    ZipOutputStream zos = new ZipOutputStream(baos);

    for (File f : files) {

        zos.putNextEntry(new ZipEntry(f.getName()));

        zos.write(getBytesFromFile(f.getAbsoluteFile()));

        zos.closeEntry();
    }

    zos.flush();
    baos.flush();
    zos.close();
    baos.close();

    return baos.toByteArray();
}

From source file:com.juancarlosroot.threads.SimulatedUser.java

public static void addToZipFile(String fileName, ZipOutputStream zos)
        throws FileNotFoundException, IOException {
    File file = new File(fileName);
    FileInputStream fis = new FileInputStream(file);
    ZipEntry zipEntry = new ZipEntry(fileName);
    zos.putNextEntry(zipEntry);//from   www  .  j a va2  s. c om

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

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

From source file:com.rackspacecloud.client.cloudfiles.sample.FilesCopy.java

public static File zipFile(File f) throws IOException {
    byte[] buf = new byte[1024];
    int len;//from   w w w  . ja  v a  2  s.  c  o  m

    // Create the ZIP file
    String filenameWithZipExt = f.getName() + ZIPEXTENSION;
    File zippedFile = new File(FilenameUtils.concat(SYSTEM_TMP.getAbsolutePath(), filenameWithZipExt));

    ZipOutputStream out = new ZipOutputStream(new FileOutputStream(zippedFile));
    FileInputStream in = new FileInputStream(f);

    // Add ZIP entry to output stream.
    out.putNextEntry(new ZipEntry(f.getName()));

    // Transfer bytes from the file to the ZIP file
    while ((len = in.read(buf)) > 0) {
        out.write(buf, 0, len);
    }

    // Complete the entry
    out.closeEntry();
    out.flush();

    in.close();
    out.close();

    return zippedFile;
}

From source file:Main.java

private static void zipDir(String dir, ZipOutputStream out) throws IOException {
    File directory = new File(dir);

    URI base = directory.toURI();

    ArrayList<File> filesToZip = new ArrayList<File>();

    GetFiles(directory, filesToZip);//from   w ww  .j  a  v a  2s  .c om

    for (int i = 0; i < filesToZip.size(); ++i) {
        FileInputStream in = new FileInputStream(filesToZip.get(i));

        String name = base.relativize(filesToZip.get(i).toURI()).getPath();

        out.putNextEntry(new ZipEntry(name));

        byte[] buf = new byte[4096];
        int bytes = 0;

        while ((bytes = in.read(buf)) != -1) {
            out.write(buf, 0, bytes);
        }

        out.closeEntry();

        in.close();
    }

    out.finish();
    out.flush();
}

From source file:com.isomorphic.maven.util.ArchiveUtils.java

/**
 * Steps common to archiving both zip and jar files, which include reading files from disk and using
 * their contents to create {@link ZipEntry ZipEntries} and writing them to a ZipOutputStream.
 * //from  w  ww  .  j  a  va2 s.  c o m
 * @param root
 * @param source
 * @param target
 * @throws IOException
 */
private static void zip(File root, File source, ZipOutputStream target) throws IOException {
    String relativePath = root.toURI().relativize(source.toURI()).getPath().replace("\\", "/");

    BufferedInputStream in = null;
    try {
        if (source.isDirectory()) {

            if (!relativePath.endsWith("/")) {
                relativePath += "/";
            }
            ZipEntry entry = ZipEntryFactory.get(target, relativePath);
            entry.setTime(source.lastModified());
            target.putNextEntry(entry);
            target.closeEntry();

            for (File nestedFile : source.listFiles()) {
                zip(root, nestedFile, target);
            }
            return;
        }

        ZipEntry entry = ZipEntryFactory.get(target, relativePath);
        entry.setTime(source.lastModified());
        target.putNextEntry(entry);
        in = new BufferedInputStream(FileUtils.openInputStream(source));
        IOUtils.copy(in, target);
        target.closeEntry();

    } finally {
        IOUtils.closeQuietly(in);
    }
}