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:cascading.tap.hadoop.ZipInputFormatTest.java

public void testSplits() throws Exception {
    JobConf job = new JobConf();
    FileSystem currentFs = FileSystem.get(job);

    Path file = new Path(workDir, "test.zip");

    Reporter reporter = Reporter.NULL;/*ww w  .ja  v a 2  s  .  c  om*/

    int seed = new Random().nextInt();
    LOG.info("seed = " + seed);
    Random random = new Random(seed);
    FileInputFormat.setInputPaths(job, file);

    for (int entries = 1; entries < MAX_ENTRIES; entries += random.nextInt(MAX_ENTRIES / 10) + 1) {
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        ZipOutputStream zos = new ZipOutputStream(byteArrayOutputStream);
        long length = 0;

        LOG.debug("creating; zip file with entries = " + entries);

        // for each entry in the zip file
        for (int entryCounter = 0; entryCounter < entries; entryCounter++) {
            // construct zip entries splitting MAX_LENGTH between entries
            long entryLength = MAX_LENGTH / entries;
            ZipEntry zipEntry = new ZipEntry("/entry" + entryCounter + ".txt");
            zipEntry.setMethod(ZipEntry.DEFLATED);
            zos.putNextEntry(zipEntry);

            for (length = entryCounter * entryLength; length < (entryCounter + 1) * entryLength; length++) {
                zos.write(Long.toString(length).getBytes());
                zos.write("\n".getBytes());
            }

            zos.flush();
            zos.closeEntry();
        }

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

        currentFs.delete(file, true);

        OutputStream outputStream = currentFs.create(file);

        byteArrayOutputStream.writeTo(outputStream);
        outputStream.close();

        ZipInputFormat format = new ZipInputFormat();
        format.configure(job);
        LongWritable key = new LongWritable();
        Text value = new Text();
        InputSplit[] splits = format.getSplits(job, 100);

        BitSet bits = new BitSet((int) length);
        for (int j = 0; j < splits.length; j++) {
            LOG.debug("split[" + j + "]= " + splits[j]);
            RecordReader<LongWritable, Text> reader = format.getRecordReader(splits[j], job, reporter);

            try {
                int count = 0;

                while (reader.next(key, value)) {
                    int v = Integer.parseInt(value.toString());
                    LOG.debug("read " + v);

                    if (bits.get(v))
                        LOG.warn("conflict with " + v + " in split " + j + " at position " + reader.getPos());

                    assertFalse("key in multiple partitions.", bits.get(v));
                    bits.set(v);
                    count++;
                }

                LOG.debug("splits[" + j + "]=" + splits[j] + " count=" + count);
            } finally {
                reader.close();
            }
        }

        assertEquals("some keys in no partition.", length, bits.cardinality());
    }
}

From source file:net.sourceforge.floggy.persistence.pool.ZipOutputPool.java

/**
 * DOCUMENT ME!// w  w  w .  j  a v a 2  s.  c o  m
*
* @param resourceStream DOCUMENT ME!
* @param fileName DOCUMENT ME!
*
* @throws IOException DOCUMENT ME!
*/
public void addResource(InputStream resourceStream, String fileName) throws IOException {
    fileName = fileName.replace(File.separatorChar, '/');

    if (fileName.startsWith("/")) {
        fileName = fileName.substring(1);
    }

    ZipEntry entry = new ZipEntry(fileName);
    out.putNextEntry(entry);
    IOUtils.copy(resourceStream, out);
    out.closeEntry();
}

From source file:com.joliciel.csvLearner.CSVEventListWriter.java

public void writeFile(GenericEvents events) {
    try {/* w w w .  j av  a  2 s . c o  m*/
        LOG.debug("writeFile: " + file.getName());
        file.delete();
        file.createNewFile();
        if (file.getName().endsWith(".zip"))
            isZip = true;
        Writer writer = null;
        ZipOutputStream zos = null;
        try {
            if (isZip) {
                zos = new ZipOutputStream(new FileOutputStream(file, false));
                writer = new BufferedWriter(new OutputStreamWriter(zos));
            } else {
                writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file, false), "UTF8"));
            }
            Set<String> features = new TreeSet<String>();
            if (!filePerEvent) {
                if (isZip) {
                    zos.putNextEntry(new ZipEntry(
                            file.getName().substring(0, file.getName().lastIndexOf('.')) + ".csv"));
                }

                for (GenericEvent event : events) {
                    if (LOG.isTraceEnabled())
                        LOG.trace("Writing event: " + event.getIdentifier());
                    for (String feature : event.getFeatures()) {
                        int classIndex = feature.indexOf(CSVLearner.NOMINAL_MARKER);
                        if (classIndex < 0 || denominalise)
                            features.add(feature);
                        else
                            features.add(feature.substring(0, classIndex));
                    }
                }

                writer.append("ID,");

                if (includeOutcomes)
                    writer.append("outcome,");
                for (String feature : features) {
                    writer.append(CSVFormatter.format(feature) + ",");
                }

                writer.append("\n");
                writer.flush();
            }

            for (GenericEvent event : events) {
                if (filePerEvent) {
                    features = new TreeSet<String>();
                    for (String feature : event.getFeatures()) {
                        int classIndex = feature.indexOf(CSVLearner.NOMINAL_MARKER);
                        if (classIndex < 0 || denominalise)
                            features.add(feature);
                        else
                            features.add(feature.substring(0, classIndex));
                    }

                    if (isZip)
                        zos.putNextEntry(new ZipEntry(event.getIdentifier() + ".csv"));
                    writer.append("ID,");
                    if (includeOutcomes)
                        writer.append("outcome,");

                    for (String feature : features) {
                        writer.append(CSVFormatter.format(feature) + ",");
                    }

                    writer.append("\n");
                    writer.flush();
                }
                writer.append(CSVFormatter.format(identifierPrefix + event.getIdentifier()) + ",");
                if (includeOutcomes)
                    writer.append(CSVFormatter.format(event.getOutcome()) + ",");

                for (String feature : features) {
                    Integer featureIndexObj = event.getFeatureIndex(feature);
                    int featureIndex = featureIndexObj == null ? -1 : featureIndexObj.intValue();

                    if (featureIndex < 0) {
                        writer.append(missingValueString + ",");
                    } else {
                        String eventFeature = event.getFeatures().get(featureIndex);
                        if (!eventFeature.equals(feature)) {
                            int classIndex = eventFeature.indexOf(CSVLearner.NOMINAL_MARKER);
                            String clazz = eventFeature
                                    .substring(classIndex + CSVLearner.NOMINAL_MARKER.length());
                            writer.append(CSVFormatter.format(clazz) + ",");
                        } else {
                            double value = event.getWeights().get(featureIndex);
                            writer.append(CSVFormatter.format(value) + ",");
                        }
                    }
                }
                writer.append("\n");
                writer.flush();
                if (filePerEvent && isZip)
                    zos.closeEntry();
            }
            if (!filePerEvent && isZip)
                zos.closeEntry();
        } finally {
            if (zos != null) {
                zos.flush();
                zos.close();
            }
            if (writer != null) {
                writer.flush();
                writer.close();
            }
        }
    } catch (IOException ioe) {
        throw new RuntimeException(ioe);
    }
}

From source file:edu.umd.cs.marmoset.modelClasses.ZipFileAggregator.java

public void addPlainFileFromBytes(String name, byte[] bytes) throws IOException, BadInputZipFileException {
    // Create output entry
    ZipEntry outputEntry = new ZipEntry(name);
    zipOutput.putNextEntry(outputEntry);

    zipOutput.write(bytes);/* ww w. j ava 2  s .c o  m*/
    zipOutput.closeEntry();
}

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;// w  w w. j  av  a2s .c  o  m
        while ((len = input.read(buf)) > 0) {
            zipOut.write(buf, 0, len);
        }
        zipOut.closeEntry();
        input.close();
    }
    zipOut.finish();
    mFinished = true;
}

From source file:org.cloudfoundry.client.lib.io.DynamicZipInputStream.java

@Override
protected boolean writeMoreData() throws IOException {

    // Write data from the current stream if possible
    int count = entryStream.read(buffer);
    if (count != -1) {
        zipStream.write(buffer, 0, count);
        return true;
    }//from   w ww. j  a va2  s . co  m

    // Close any open entry
    if (entryStream != EMPTY_STREAM) {
        zipStream.closeEntry();
        entryStream.close();
        entryStream = EMPTY_STREAM;
    }

    // Move to the next entry if there is one (no need to write data as returning true causes another call)
    if (entries.hasNext()) {
        fileCount++;
        Entry entry = entries.next();
        zipStream.putNextEntry(new ZipEntry(entry.getName()));
        entryStream = entry.getInputStream();
        if (entryStream == null) {
            entryStream = EMPTY_STREAM;
        }
        return true;
    }

    // If no files were added to the archive add an empty one
    if (fileCount == 0) {
        fileCount++;
        zipStream.putNextEntry(new ZipEntry("__empty__"));
        entryStream = EMPTY_STREAM;
        return true;
    }

    // No more entries, close and flush the stream
    zipStream.flush();
    zipStream.close();
    return false;
}

From source file:ch.rgw.compress.CompEx.java

public static byte[] CompressZIP(InputStream in) throws Exception {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    byte[] buf = new byte[8192];
    baos.write(buf, 0, 4); // Lnge des Originalstroms
    ZipOutputStream zo = new ZipOutputStream(baos);
    zo.putNextEntry(new ZipEntry("Data"));
    int l;// w w  w .j  a  v  a  2  s  .c om
    long total = 0;
    ;
    while ((l = in.read(buf, 0, buf.length)) != -1) {
        zo.write(buf, 0, l);
        total += l;
    }
    zo.close();
    byte[] ret = baos.toByteArray();
    // Die hchstwertigen 3 Bit als Typmarker setzen
    total &= 0x1fffffff;
    total |= ZIP;
    BinConverter.intToByteArray((int) total, ret, 0);
    return ret;
}

From source file:com.jaspersoft.jasperserver.export.io.ZipOutput.java

public OutputStream getFileOutputStream(String path) throws IOException {
    String zipPath = getZipPath(path);
    ZipEntry fileEntry = new ZipEntry(zipPath);
    zipOut.putNextEntry(fileEntry);//from  w w w  . ja v  a  2 s  . c o m
    EntryOutputStream entryOut = new EntryOutputStream();
    return entryOut;
}

From source file:gov.nih.nci.cabig.caaers.web.ae.AdditionalInformationDocumentZipDownloadController.java

@Override
protected ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse response)
        throws ServletRequestBindingException {

    Integer additionalInformationId = ServletRequestUtils.getRequiredIntParameter(request,
            "additionalInformationId");

    List<AdditionalInformationDocument> additionalInformationDocuments = additionalInformationDocumentService
            .findByAdditionalInformationId(additionalInformationId);

    File tempFile = null;//from  w ww  .  j a va 2 s .  com
    ZipOutputStream zos = null;
    FileOutputStream fos = null;

    List<String> zipEntriesName = new ArrayList<String>();
    try {
        tempFile = File.createTempFile(
                "additionalInformationFile" + System.currentTimeMillis() + RandomUtils.nextInt(1000), ".zip");

        fos = new FileOutputStream(tempFile);
        zos = new ZipOutputStream(fos);

        for (AdditionalInformationDocument additionalInformationDocument : additionalInformationDocuments) {
            String name = getUniqueZipEntryName(additionalInformationDocument, zipEntriesName);
            zipEntriesName.add(name);
            ZipEntry zipEntry = new ZipEntry(name);
            zos.putNextEntry(zipEntry);
            IOUtils.copy(new FileInputStream(additionalInformationDocument.getFile()), zos);
            zos.closeEntry();
        }

        zos.flush();

    } catch (Exception e) {

        log.error("Unable to create temp file", e);
        return null;
    } finally {
        if (zos != null)
            IOUtils.closeQuietly(zos);
        if (fos != null)
            IOUtils.closeQuietly(fos);
    }

    if (tempFile != null) {

        FileInputStream fis = null;
        OutputStream out = null;
        try {
            fis = new FileInputStream(tempFile);
            out = response.getOutputStream();

            response.setContentType("application/x-download");
            response.setHeader("Content-Disposition",
                    "attachment; filename=" + additionalInformationId + ".zip");
            response.setHeader("Content-length", String.valueOf(tempFile.length()));
            response.setHeader("Pragma", "private");
            response.setHeader("Cache-control", "private, must-revalidate");

            IOUtils.copy(fis, out);
            out.flush();
        } catch (Exception e) {
            log.error("Error while reading zip file ", e);
        } finally {
            IOUtils.closeQuietly(fis);
            IOUtils.closeQuietly(out);
        }

        FileUtils.deleteQuietly(tempFile);
    }
    return null;
}

From source file:com.mobeelizer.java.sync.MobeelizerOutputData.java

public void close() {
    InputStream dataInputStream = null;

    try {/*from   w w w  . ja  va  2s. c  om*/
        dataOutputStream.close();

        zip.putNextEntry(new ZipEntry(MobeelizerInputData.DATA_ENTRY_NAME));
        dataInputStream = new FileInputStream(dataFile);
        copy(dataInputStream, zip);
        dataInputStream.close();
        zip.closeEntry();

        zip.putNextEntry(new ZipEntry(MobeelizerInputData.DELETED_FILES_ENTRY_NAME));
        writeLines(deletedFiles, "\n", zip);
        zip.closeEntry();

        zip.close();
    } catch (IOException e) {
        closeQuietly(zip);
        closeQuietly(dataInputStream);
        closeQuietly(dataOutputStream);
        throw new IllegalStateException(e.getMessage(), e);
    }
}