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:abfab3d.io.output.STSWriter.java

/**
 * Writes a grid out to an svx file//from w  w w.  j  a v  a  2 s . c o m
 * @param grid
 * @param os
 */
public void write(AttributeGrid grid, MaterialMaker[] makers, String[] finish, ZipOutputStream os) {
    try {

        ZipEntry zentry = new ZipEntry("manifest.xml");
        os.putNextEntry(zentry);
        writeManifest(grid, makers, finish, os);
        os.closeEntry();

        double maxDecimationError = errorFactor * grid.getVoxelSize() * grid.getVoxelSize();

        int len = makers.length;

        for (int i = 0; i < len; i++) {
            ZipEntry ze = new ZipEntry("part" + i + "." + format);
            ((ZipOutputStream) os).putNextEntry(ze);

            MeshMakerMT meshmaker = new MeshMakerMT();
            meshmaker.setBlockSize(30);
            meshmaker.setThreadCount(threadCount);
            meshmaker.setSmoothingWidth(smoothingWidth);
            meshmaker.setMaxDecimationError(maxDecimationError);
            meshmaker.setMaxDecimationCount(10);
            meshmaker.setMaxAttributeValue(255);
            meshmaker.setDensityMaker(makers[i].getDensityMaker());

            IndexedTriangleSetBuilder its = new IndexedTriangleSetBuilder(160000);
            meshmaker.makeMesh(grid, its);

            System.out.println("Vertices: " + its.getVertexCount() + " faces: " + its.getFaceCount());

            WingedEdgeTriangleMesh mesh = new WingedEdgeTriangleMesh(its.getVertices(), its.getFaces());

            if (minPartVolume > 0 || maxPartsCount < Integer.MAX_VALUE) {
                ShellResults sr = GridSaver.getLargestShells(mesh, maxPartsCount, minPartVolume);
                mesh = sr.getLargestShell();
                int regions_removed = sr.getShellsRemoved();
                System.out.println("Regions removed: " + regions_removed);
            }

            if (format.equals("x3d") || format.equals("x3dv") || format.equals("x3db")) {

                double[] bounds_min = new double[3];
                double[] bounds_max = new double[3];

                grid.getGridBounds(bounds_min, bounds_max);
                double max_axis = Math.max(bounds_max[0] - bounds_min[0], bounds_max[1] - bounds_min[1]);
                max_axis = Math.max(max_axis, bounds_max[2] - bounds_min[2]);

                double z = 2 * max_axis / Math.tan(Math.PI / 4);
                float[] pos = new float[] { 0, 0, (float) z };

                BinaryContentHandler x3dWriter = createX3DWriter(os);

                HashMap<String, Object> x3dParams = new HashMap<String, Object>();
                GridSaver.writeMesh(mesh, 10, x3dWriter, x3dParams, true);
                x3dWriter.endDocument();
            } else if (format.equals("stl")) {
                STLWriter stl = new STLWriter(os, mesh.getTriangleCount());
                mesh.getTriangles(stl);
            }

            ((ZipOutputStream) os).closeEntry();
        }
    } catch (IOException ioe) {
        ioe.printStackTrace();
    }
}

From source file:com.recomdata.transmart.data.export.util.ZipUtil.java

static private void addFileToZip(String path, String srcFile, ZipOutputStream zip) throws Exception {

    File folder = new File(srcFile);
    if (folder.isDirectory()) {
        addFolderToZip(path, srcFile, zip);
    } else {/*from w  w  w  .j  a  v a  2 s  . co m*/
        byte[] buf = new byte[BUFFER_SIZE];
        int len;
        FileInputStream in = new FileInputStream(srcFile);
        zip.putNextEntry(new ZipEntry(path + "/" + folder.getName()));
        while ((len = in.read(buf)) > 0) {
            zip.write(buf, 0, len);
        }
    }
}

From source file:hudson.plugins.doclinks.artifacts.testtools.TestZipBuilder.java

private void compress(ZipOutputStream zos, File dir, String relative) throws IOException {
    for (String filename : dir.list()) {
        File file = new File(dir, filename);
        String path = StringUtils.isEmpty(relative) ? filename : String.format("%s/%s", relative, filename);
        if (file.isDirectory()) {
            if (!noEntryForDirectories) {
                ZipEntry entry = new ZipEntry(String.format("%s/", path));
                zos.putNextEntry(entry);
            }/*from   www.j  av a 2  s. c  o m*/
            compress(zos, file, path);
        } else {
            ZipEntry entry = new ZipEntry(path);
            zos.putNextEntry(entry);
            FileInputStream is = null;
            try {
                is = new FileInputStream(file);
                IOUtils.copy(is, zos);
            } finally {
                if (is != null) {
                    is.close();
                }
            }
        }
    }
}

From source file:de.blizzy.backup.Utils.java

public static void zipFile(File source, File target) throws IOException {
    ZipOutputStream out = null;/*from  ww  w  .j av a2s .com*/
    try {
        out = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(target)));
        out.setLevel(Deflater.BEST_COMPRESSION);

        ZipEntry entry = new ZipEntry(source.getName());
        entry.setTime(source.lastModified());
        out.putNextEntry(entry);

        Files.copy(source.toPath(), out);
    } finally {
        IOUtils.closeQuietly(out);
    }
}

From source file:cz.zcu.kiv.eegdatabase.logic.zip.ZipGenerator.java

public File generate(Experiment exp, MetadataCommand mc, Set<DataFile> dataFiles, byte[] licenseFile,
        String licenseFileName) throws Exception, SQLException, IOException {

    ZipOutputStream zipOutputStream = null;
    FileOutputStream fileOutputStream = null;
    File tempZipFile = null;/*from ww  w  . j av a2 s  . co m*/

    try {
        log.debug("creating output stream");
        // create temp zip file
        tempZipFile = File.createTempFile("experimentDownload_", ".zip");
        // open stream to temp zip file
        fileOutputStream = new FileOutputStream(tempZipFile);
        // prepare zip stream
        zipOutputStream = new ZipOutputStream(fileOutputStream);

        log.debug("transforming metadata from database to xml file");
        OutputStream meta = getTransformer().transformElasticToXml(exp);
        Scenario scen = exp.getScenario();
        log.debug("getting scenario file");

        byte[] xmlMetadata = null;
        if (meta instanceof ByteArrayOutputStream) {
            xmlMetadata = ((ByteArrayOutputStream) meta).toByteArray();
        }

        ZipEntry entry;

        if (licenseFileName != null && !licenseFileName.isEmpty()) {
            zipOutputStream.putNextEntry(entry = new ZipEntry("License/" + licenseFileName));
            IOUtils.copyLarge(new ByteArrayInputStream(licenseFile), zipOutputStream);
            zipOutputStream.closeEntry();
        }

        if (mc.isScenFile() && scen.getScenarioFile() != null) {
            try {

                log.debug("saving scenario file (" + scen.getScenarioName() + ") into a zip file");
                entry = new ZipEntry("Scenario/" + scen.getScenarioName());
                zipOutputStream.putNextEntry(entry);
                IOUtils.copyLarge(scen.getScenarioFile().getBinaryStream(), zipOutputStream);
                zipOutputStream.closeEntry();

            } catch (Exception ex) {
                log.error(ex);
            }
        }

        if (xmlMetadata != null) {
            log.debug("saving xml file of metadata to zip file");
            entry = new ZipEntry(getMetadata() + ".xml");
            zipOutputStream.putNextEntry(entry);
            zipOutputStream.write(xmlMetadata);
            zipOutputStream.closeEntry();
        }

        for (DataFile dataFile : dataFiles) {
            entry = new ZipEntry(getDataZip() + "/" + dataFile.getFilename());

            if (dataFile.getFileContent().length() > 0) {

                log.debug("saving data file to zip file");

                try {

                    zipOutputStream.putNextEntry(entry);

                } catch (ZipException ex) {

                    String[] partOfName = dataFile.getFilename().split("[.]");
                    String filename;
                    if (partOfName.length < 2) {
                        filename = partOfName[0] + "" + fileCounter;
                    } else {
                        filename = partOfName[0] + "" + fileCounter + "." + partOfName[1];
                    }
                    entry = new ZipEntry(getDataZip() + "/" + filename);
                    zipOutputStream.putNextEntry(entry);
                    fileCounter++;
                }

                IOUtils.copyLarge(dataFile.getFileContent().getBinaryStream(), zipOutputStream);
                zipOutputStream.closeEntry();
            }
        }

        log.debug("returning output stream of zip file");
        return tempZipFile;

    } finally {

        zipOutputStream.flush();
        zipOutputStream.close();
        fileOutputStream.flush();
        fileOutputStream.close();
        fileCounter = 0;

    }
}

From source file:fll.web.GatherBugReport.java

/**
 * Add the database to the zipfile.//from ww w . j ava 2 s  . co m
 * 
 * @throws SQLException
 */
private static void addDatabase(final ZipOutputStream zipOut, final Connection connection,
        final Document challengeDocument) throws IOException, SQLException {

    ZipOutputStream dbZipOut = null;
    FileInputStream fis = null;
    try {
        final File temp = File.createTempFile("database", ".flldb");

        dbZipOut = new ZipOutputStream(new FileOutputStream(temp));
        DumpDB.dumpDatabase(dbZipOut, connection, challengeDocument);
        dbZipOut.close();

        zipOut.putNextEntry(new ZipEntry("database.flldb"));
        fis = new FileInputStream(temp);
        IOUtils.copy(fis, zipOut);
        fis.close();

        if (!temp.delete()) {
            temp.deleteOnExit();
        }

    } finally {
        IOUtils.closeQuietly(dbZipOut);
        IOUtils.closeQuietly(fis);
    }

}

From source file:apim.restful.importexport.utils.ArchiveGeneratorUtil.java

/**
 * Add files of the directory to the archive
 *
 * @param directoryToZip  Location of the archive
 * @param file            File to be included in the archive
 * @param zipOutputStream Output stream/*from w w w .j a va  2  s.com*/
 * @throws APIExportException If an error occurs while writing files to the archive
 */
private static void addToArchive(File directoryToZip, File file, ZipOutputStream zipOutputStream)
        throws APIExportException {

    FileInputStream fileInputStream = null;
    try {
        fileInputStream = new FileInputStream(file);

        // Get relative path from archive directory to the specific file
        String zipFilePath = file.getCanonicalPath().substring(directoryToZip.getCanonicalPath().length() + 1,
                file.getCanonicalPath().length());
        if (File.separatorChar != '/')
            zipFilePath = zipFilePath.replace(File.separatorChar,
                    APIImportExportConstants.ARCHIVE_PATH_SEPARATOR);
        ZipEntry zipEntry = new ZipEntry(zipFilePath);
        zipOutputStream.putNextEntry(zipEntry);

        IOUtils.copy(fileInputStream, zipOutputStream);

        zipOutputStream.closeEntry();
    } catch (IOException e) {
        log.error("I/O error while writing files to archive" + e.getMessage());
        throw new APIExportException("I/O error while writing files to archive", e);
    } finally {
        IOUtils.closeQuietly(fileInputStream);
    }
}

From source file:com.eviware.soapui.integration.exporter.ProjectExporter.java

/**
 * Compress temporary directory and save it on given path.
 * /*from w w  w  .  ja va  2 s.c o m*/
 * @param exportPath
 * @return
 * @throws IOException
 */
private boolean packageAll(String exportPath) {
    if (!exportPath.endsWith(".zip"))
        exportPath = exportPath + ".zip";

    BufferedInputStream origin = null;
    ZipOutputStream out;
    boolean result = true;
    try {
        FileOutputStream dest = new FileOutputStream(exportPath);
        out = new ZipOutputStream(new BufferedOutputStream(dest));
        byte data[] = new byte[BUFFER];
        // get a list of files from current directory

        String files[] = tmpDir.list();

        for (int i = 0; i < files.length; i++) {
            //            System.out.println( "Adding: " + files[i] );
            FileInputStream fi = new FileInputStream(new File(tmpDir, files[i]));
            origin = new BufferedInputStream(fi, BUFFER);
            ZipEntry entry = new ZipEntry(files[i]);
            out.putNextEntry(entry);
            int count;
            while ((count = origin.read(data, 0, BUFFER)) != -1) {
                out.write(data, 0, count);
            }
            origin.close();
        }
        out.close();
    } catch (IOException e) {
        // TODO: handle exception
        result = false;
    }
    return result;
}

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

private void consultarFirma(int fileNameId) {
    try {//from   www . j a  va  2s. c  om
        FileOutputStream fos = new FileOutputStream(
                userFolderPath + "/" + Integer.toString(fileNameId) + "Firmada.zip");
        ZipOutputStream zos = new ZipOutputStream(fos);

        String file1Name = userFolderPath + "/" + Integer.toString(fileNameId) + "Firmada.png";
        File image = new File(file1Name);

        ZipEntry zipEntry = new ZipEntry(image.getName());
        zos.putNextEntry(zipEntry);
        FileInputStream fileInputStream = new FileInputStream(image);

        byte[] buf = new byte[2048];
        int bytesRead;

        while ((bytesRead = fileInputStream.read(buf)) > 0) {
            zos.write(buf, 0, bytesRead);
        }
        zos.closeEntry();
        zos.close();
        fos.close();

        Path path = Paths.get(userFolderPath + "/" + Integer.toString(fileNameId) + "Firmada.zip");

        byte[] data = Files.readAllBytes(path);
        byte[] byteArray = Base64.encodeBase64(data);
        String b64 = new String(byteArray);

        System.out.println(consultaFirma(b64, Integer.toString(fileNameId) + "Firmada"));

        nFilesVerified++;
        System.out.println("User : " + idSimulatedUser + " FIRMA VERIFICADA");
    } catch (IOException ex) {
        Logger.getLogger(SimulatedUser.class.getName()).log(Level.SEVERE, null, ex);
        nNotFileFound++;
    } catch (IOException_Exception ex) {
        Logger.getLogger(SimulatedUser.class.getName()).log(Level.SEVERE, null, ex);
        nVerifyErrors++;
    }
}

From source file:org.envirocar.app.util.Util.java

/**
 * Zips a list of files into the target archive.
 * //w w w  .ja v  a2s .  c  o  m
 * @param files
 *            the list of files of the target archive
 * @param target
 *            the target filename
 * @throws IOException 
 */
public static void zipNative(List<File> files, String target) throws IOException {
    ZipOutputStream zos = null;
    try {
        File targetFile = new File(target);
        FileOutputStream dest = new FileOutputStream(targetFile);

        zos = new ZipOutputStream(new BufferedOutputStream(dest));

        for (File f : files) {
            byte[] bytes = readFileContents(f).toByteArray();
            ZipEntry entry = new ZipEntry(f.getName());
            zos.putNextEntry(entry);
            zos.write(bytes);
            zos.closeEntry();
        }

    } catch (IOException e) {
        throw e;
    } finally {
        try {
            if (zos != null)
                zos.close();
        } catch (IOException e) {
            logger.warn(e.getMessage(), e);
        }
    }

}