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:org.apache.uima.ruta.resource.TreeWordList.java

private void writeCompressedTWLFile(TextNode root, String path, String encoding) throws IOException {
    FileOutputStream fos = new FileOutputStream(path);
    BufferedOutputStream bos = new BufferedOutputStream(fos);
    ZipOutputStream zos = new ZipOutputStream(bos);
    OutputStreamWriter writer = new OutputStreamWriter(zos, encoding);
    zos.putNextEntry(new ZipEntry(path));
    writeTWLFile(root, writer);/* w w w  .  j av a2s.  c  o  m*/
    writer.flush();
    zos.closeEntry();
    writer.close();
}

From source file:com.yunmel.syncretic.utils.io.IOUtils.java

/**
 * ZIP?/*from www. j  a va  2s  . com*/
 * 
 * @param dirPath 
 * @param file 
 * @param zouts ?
 */
public static void zipFilesToZipFile(String dirPath, File file, ZipOutputStream zouts) {
    FileInputStream fin = null;
    ZipEntry entry = null;
    // ?
    byte[] buf = new byte[4096];
    int readByte = 0;
    if (file.isFile()) {
        try {
            // ?
            fin = new FileInputStream(file);
            // ZipEntry
            entry = new ZipEntry(getEntryName(dirPath, file));
            // ?
            zouts.putNextEntry(entry);
            // ?
            while ((readByte = fin.read(buf)) != -1) {
                zouts.write(buf, 0, readByte);
            }
            zouts.closeEntry();
            fin.close();
            System.out.println(" " + file.getAbsolutePath() + " zip!");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}

From source file:eu.delving.services.controller.DataSetController.java

private void writeSipZip(String dataSetSpec, OutputStream outputStream, String accessKey) throws IOException,
        MappingNotFoundException, AccessKeyException, XMLStreamException, MetadataException, MappingException {
    MetaRepo.DataSet dataSet = metaRepo.getDataSet(dataSetSpec);
    if (dataSet == null) {
        throw new IOException("Data Set not found"); // IOException?
    }/*from   ww  w . j ava  2s  .  c  o m*/
    ZipOutputStream zos = new ZipOutputStream(outputStream);
    zos.putNextEntry(new ZipEntry(FileStore.FACTS_FILE_NAME));
    Facts facts = Facts.fromBytes(dataSet.getDetails().getFacts());
    facts.setDownloadedSource(true);
    zos.write(Facts.toBytes(facts));
    zos.closeEntry();
    zos.putNextEntry(new ZipEntry(FileStore.SOURCE_FILE_NAME));
    String sourceHash = writeSourceStream(dataSet, zos, accessKey);
    zos.closeEntry();
    for (MetaRepo.Mapping mapping : dataSet.mappings().values()) {
        RecordMapping recordMapping = mapping.getRecordMapping();
        zos.putNextEntry(
                new ZipEntry(String.format(FileStore.MAPPING_FILE_PATTERN, recordMapping.getPrefix())));
        RecordMapping.write(recordMapping, zos);
        zos.closeEntry();
    }
    zos.finish();
    zos.close();
    dataSet.setSourceHash(sourceHash, true);
    dataSet.save();
}

From source file:it.geosolutions.tools.compress.file.Compressor.java

/**
 * @param outputDir/*from  ww  w .  j  a  va 2 s  .  c  om*/
 *            The directory where the zipfile will be created
 * @param zipFileBaseName
 *            The basename of hte zip file (i.e.: a .zip will be appended)
 * @param folder
 *            The folder that will be compressed
 * @return The created zipfile, or null if an error occurred.
 * @deprecated TODO UNTESTED
 */
public static File deflate(final File outputDir, final String zipFileBaseName, final File folder) {
    // Create a buffer for reading the files
    byte[] buf = new byte[4096];

    // Create the ZIP file
    final File outZipFile = new File(outputDir, zipFileBaseName + ".zip");
    if (outZipFile.exists()) {
        if (LOGGER.isInfoEnabled())
            LOGGER.info("The output file already exists: " + outZipFile);
        return outZipFile;
    }
    ZipOutputStream out = null;
    BufferedOutputStream bos = null;
    FileOutputStream fos = null;
    try {
        fos = new FileOutputStream(outZipFile);
        bos = new BufferedOutputStream(fos);
        out = new ZipOutputStream(bos);
        Collector c = new Collector(null);
        List<File> files = c.collect(folder);
        // Compress the files
        for (File file : files) {
            FileInputStream in = null;
            try {
                in = new FileInputStream(file);
                if (file.isDirectory()) {
                    out.putNextEntry(new ZipEntry(Path.toRelativeFile(folder, file).getPath()));
                } else {
                    // Add ZIP entry to output stream.
                    out.putNextEntry(new ZipEntry(FilenameUtils.getBaseName(file.getName())));
                    // Transfer bytes from the file to the ZIP file
                    int len;
                    while ((len = in.read(buf)) > 0) {
                        out.write(buf, 0, len);
                    }
                }

            } finally {
                try {
                    // Complete the entry
                    out.closeEntry();
                } catch (IOException e) {
                }
                IOUtils.closeQuietly(in);
            }

        }

    } catch (IOException e) {
        LOGGER.error(e.getLocalizedMessage(), e);
        return null;
    } finally {

        // Complete the ZIP file
        IOUtils.closeQuietly(bos);
        IOUtils.closeQuietly(fos);
        IOUtils.closeQuietly(out);
    }

    return outZipFile;
}

From source file:org.dataconservancy.packaging.tool.model.impl.PackageStateBuilderImpl.java

private void writeToZipOutputStream(ZipOutputStream outputStream, File fileToWrite) throws IOException {
    //Add packageToolMetadata to the ZipOutputStream

    //Write bagInfoFile to Package state file
    outputStream.putNextEntry(new ZipEntry(fileToWrite.getName()));
    FileInputStream fis = new FileInputStream(fileToWrite);
    byte[] buffer = new byte[1024];

    int length;// w  w  w  . j ava  2s.  c  o  m
    while ((length = fis.read(buffer)) > 0) {
        outputStream.write(buffer, 0, length);
    }
    outputStream.closeEntry();
    // close the InputStream
    fis.close();
}

From source file:com.navjagpal.fileshare.StreamingZipEntity.java

public void writeTo(OutputStream out) throws IOException {
    Cursor c = mContentResolver.query(FileSharingProvider.Files.CONTENT_URI,
            new String[] { FileSharingProvider.Files.Columns.DISPLAY_NAME,
                    FileSharingProvider.Files.Columns._DATA },
            FileSharingProvider.Files.Columns.FOLDER_ID + "=?", new String[] { mFolderId }, null);
    ZipOutputStream zipOut = new ZipOutputStream(out);
    byte[] buf = new byte[BUFFER_SIZE];
    while (c.moveToNext()) {
        String filename = c.getString(c.getColumnIndex(FileSharingProvider.Files.Columns.DISPLAY_NAME));
        String data = c.getString(c.getColumnIndex(FileSharingProvider.Files.Columns._DATA));
        zipOut.putNextEntry(new ZipEntry(filename));
        InputStream input = mContentResolver.openInputStream(Uri.parse(data));
        int len;/*  ww  w. j  a v  a 2  s  .  co m*/
        while ((len = input.read(buf)) > 0) {
            zipOut.write(buf, 0, len);
        }
        zipOut.closeEntry();
        input.close();
    }
    zipOut.finish();
    mFinished = true;
}

From source file:nl.nn.adapterframework.webcontrol.DumpIbisConsole.java

public void copyResource(ZipOutputStream zipOutputStream, String resourceName, String resourceContents) {
    try {/*from w w w .  ja  va  2s. com*/
        String fileName = directoryName + resourceName;

        PrintWriter pw = new PrintWriter(
                new OutputStreamWriter(zipOutputStream, Misc.DEFAULT_INPUT_STREAM_ENCODING));
        zipOutputStream.putNextEntry(new ZipEntry(fileName));
        pw.print(resourceContents);
        pw.flush();
        zipOutputStream.closeEntry();
        log.debug("copied resource [" + resourceName + "]");
    } catch (Exception e) {
        log.error("Error copying resource", e);
    }
}

From source file:org.carlspring.strongbox.artifact.generator.NugetPackageGenerator.java

private void createMetadata(String id, String version, ZipOutputStream zos) throws IOException {
    ZipEntry ze = new ZipEntry("package/services/metadata/core-properties/metadata.psmdcp");
    zos.putNextEntry(ze);/*from  ww w. j a  va 2  s.  co  m*/

    ByteArrayInputStream is = new ByteArrayInputStream(String.format(PSMDCP_CONTENT, id, version).getBytes());
    byte[] buffer = new byte[4096];
    int len;
    while ((len = is.read(buffer)) > 0) {
        zos.write(buffer, 0, len);
    }

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

From source file:isl.FIMS.utils.Utils.java

public static void createZip(String zipFile, String sourceDirectory) {
    try {/*from  www.ja  v a  2 s.com*/
        // String zipFile = "C:/FileIO/zipdemo.zip";
        // String sourceDirectory = "C:/examples";

        //create byte buffer
        byte[] buffer = new byte[1024];
        //create object of FileOutputStream
        FileOutputStream fout = new FileOutputStream(zipFile);
        //create object of ZipOutputStream from FileOutputStream
        ZipOutputStream zout = new ZipOutputStream(fout);
        //create File object from directory name
        File dir = new File(sourceDirectory);

        //check to see if this directory exists
        if (!dir.isDirectory()) {
        } else {
            File[] files = dir.listFiles();
            for (int i = 0; i < files.length; i++) {
                if (files[i].isFile()) {
                    //create object of FileInputStream for source file
                    FileInputStream fin = new FileInputStream(files[i]);
                    zout.putNextEntry(new ZipEntry(files[i].getName()));
                    int length;
                    while ((length = fin.read(buffer)) > 0) {
                        zout.write(buffer, 0, length);
                    }
                    zout.closeEntry();
                    //close the InputStream
                    fin.close();
                }
            }
        }

        //close the ZipOutputStream
        zout.close();
    } catch (IOException ioe) {
    }
}

From source file:com.koushikdutta.superuser.MainActivity.java

void doRecoveryInstall() {
    final ProgressDialog dlg = new ProgressDialog(this);
    dlg.setTitle(R.string.installing);/*  w  w w  .j  ava2s.  co  m*/
    dlg.setMessage(getString(R.string.installing_superuser));
    dlg.setIndeterminate(true);
    dlg.show();
    new Thread() {
        void doEntry(ZipOutputStream zout, String entryName, String dest) throws IOException {
            ZipFile zf = new ZipFile(getPackageCodePath());
            ZipEntry ze = zf.getEntry(entryName);
            zout.putNextEntry(new ZipEntry(dest));
            InputStream in;
            StreamUtility.copyStream(in = zf.getInputStream(ze), zout);
            zout.closeEntry();
            in.close();
            zf.close();
        }

        public void run() {
            try {
                File zip = getFileStreamPath("superuser.zip");
                ZipOutputStream zout = new ZipOutputStream(new FileOutputStream(zip));
                doEntry(zout, "assets/update-binary", "META-INF/com/google/android/update-binary");
                doEntry(zout, "assets/install-recovery.sh", "install-recovery.sh");
                zout.close();

                ZipFile zf = new ZipFile(getPackageCodePath());
                ZipEntry ze = zf.getEntry("assets/" + getArch() + "/reboot");
                InputStream in;
                FileOutputStream reboot;
                StreamUtility.copyStream(in = zf.getInputStream(ze),
                        reboot = openFileOutput("reboot", MODE_PRIVATE));
                reboot.close();
                in.close();

                final File su = extractSu();

                String command = String.format("cat %s > /cache/superuser.zip\n", zip.getAbsolutePath())
                        + String.format("cat %s > /cache/su\n", su.getAbsolutePath())
                        + String.format("cat %s > /cache/Superuser.apk\n", getPackageCodePath())
                        + "mkdir /cache/recovery\n"
                        + "echo '--update_package=CACHE:superuser.zip' > /cache/recovery/command\n"
                        + "chmod 644 /cache/superuser.zip\n" + "chmod 644 /cache/recovery/command\n" + "sync\n"
                        + String.format("chmod 755 %s\n", getFileStreamPath("reboot").getAbsolutePath())
                        + "reboot recovery\n";
                Process p = Runtime.getRuntime().exec("su");
                p.getOutputStream().write(command.getBytes());
                p.getOutputStream().close();
                File rebootScript = getFileStreamPath("reboot.sh");
                StreamUtility.writeFile(rebootScript,
                        "reboot recovery ; " + getFileStreamPath("reboot").getAbsolutePath() + " recovery ;");
                p.waitFor();
                Runtime.getRuntime().exec(new String[] { "su", "-c", ". " + rebootScript.getAbsolutePath() });
                if (p.waitFor() != 0)
                    throw new Exception("non zero result");
            } catch (Exception ex) {
                ex.printStackTrace();
                dlg.dismiss();

                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
                        builder.setPositiveButton(android.R.string.ok, null);
                        builder.setTitle(R.string.install);
                        builder.setMessage(R.string.install_error);
                        builder.create().show();
                    }
                });
            }
        }
    }.start();
}