Example usage for java.util.zip ZipOutputStream close

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

Introduction

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

Prototype

public void close() throws IOException 

Source Link

Document

Closes the ZIP output stream as well as the stream being filtered.

Usage

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

@Override
public boolean perform(AbstractBuild<?, ?> build, Launcher launcher, BuildListener listener)
        throws InterruptedException, IOException {
    FilePath file = build.getWorkspace().child(filename);
    FilePath dir = file.getParent();//from   w  ww .j  a v a 2 s  .co m

    if (dir != null && !dir.exists()) {
        dir.mkdirs();
    }

    String seperator = System.getProperty("file.separator");
    String resourceDirName = StringUtils.join(getClass().getName().split("\\."), seperator);
    File resourceDir = null;
    try {
        resourceDir = new File(ClassLoader.getSystemResource(resourceDirName).toURI());
    } catch (URISyntaxException e) {
        listener.getLogger().println("Failed to retrieve contents to zip");
        e.printStackTrace(listener.getLogger());
    }

    OutputStream os = null;
    ZipOutputStream zos = null;

    try {
        os = file.write();
        zos = new ZipOutputStream(os);

        compress(zos, resourceDir, null);
    } finally {
        if (zos != null) {
            zos.close();
        }

        if (os != null) {
            os.close();
        }
    }

    return true;
}

From source file:com.taobao.android.builder.tools.zip.ZipUtils.java

/**
 * ?zip?solib?//from   w ww.ja  va2s .c om
 *
 * @param output
 * @param srcDir
 * @throws Exception
 */
public static void addFileAndDirectoryToZip(File output, File srcDir) throws Exception {
    if (output.isDirectory()) {
        throw new IOException("This is a directory!");
    }
    if (!output.getParentFile().exists()) {
        output.getParentFile().mkdirs();
    }

    if (!output.exists()) {
        output.createNewFile();
    }
    List fileList = getSubFiles(srcDir);
    ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(output));
    ZipEntry ze = null;
    byte[] buf = new byte[1024];
    int readLen = 0;
    for (int i = 0; i < fileList.size(); i++) {
        File f = (File) fileList.get(i);
        ze = new ZipEntry(getAbsFileName(srcDir.getPath(), f));
        ze.setSize(f.length());
        ze.setTime(f.lastModified());
        zos.putNextEntry(ze);
        InputStream is = new BufferedInputStream(new FileInputStream(f));
        while ((readLen = is.read(buf, 0, 1024)) != -1) {
            zos.write(buf, 0, readLen);
        }
        is.close();
    }
    zos.close();
}

From source file:Main.java

public void doZip(String filename, String zipfilename) throws Exception {
    byte[] buf = new byte[1024];
    FileInputStream fis = new FileInputStream(filename);
    fis.read(buf, 0, buf.length);/*from   w w w. j  a v a2s .  c o m*/

    CRC32 crc = new CRC32();
    ZipOutputStream s = new ZipOutputStream((OutputStream) new FileOutputStream(zipfilename));
    s.setLevel(6);

    ZipEntry entry = new ZipEntry(filename);
    entry.setSize((long) buf.length);
    entry.setMethod(ZipEntry.DEFLATED);
    crc.reset();
    crc.update(buf);
    entry.setCrc(crc.getValue());
    s.putNextEntry(entry);
    s.write(buf, 0, buf.length);
    s.finish();
    s.close();
}

From source file:Main.java

public void doZip(String filename, String zipfilename) throws Exception {
    byte[] buf = new byte[1024];
    FileInputStream fis = new FileInputStream(filename);
    fis.read(buf, 0, buf.length);//from w  w  w . ja  va  2 s. co m

    CRC32 crc = new CRC32();
    ZipOutputStream s = new ZipOutputStream((OutputStream) new FileOutputStream(zipfilename));
    s.setLevel(6);

    ZipEntry entry = new ZipEntry(filename);
    entry.setSize((long) buf.length);
    entry.setTime(new Date().getTime());
    crc.reset();
    crc.update(buf);
    entry.setCrc(crc.getValue());
    s.putNextEntry(entry);
    s.write(buf, 0, buf.length);
    s.finish();
    s.close();
}

From source file:be.fedict.eid.dss.portal.control.bean.UploadBean.java

@Override
public void listener(UploadEvent event) throws Exception {
    this.log.debug("listener");
    UploadItem item = event.getUploadItem();
    this.log.debug("filename: #0", item.getFileName());
    this.filename = item.getFileName();
    this.log.debug("content type: #0", item.getContentType());
    String extension = FilenameUtils.getExtension(this.filename).toLowerCase();
    this.contentType = supportedFileExtensions.get(extension);
    if (null == this.contentType) {
        /*//from  www . j a  v a  2  s. c o  m
         * Unsupported content-type is converted to a ZIP container.
         */
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        ZipOutputStream zipOutputStream = new ZipOutputStream(outputStream);
        ZipEntry zipEntry = new ZipEntry(this.filename);
        zipOutputStream.putNextEntry(zipEntry);
        IOUtils.write(item.getData(), zipOutputStream);
        zipOutputStream.close();
        this.filename = FilenameUtils.getBaseName(this.filename) + ".zip";
        this.document = outputStream.toByteArray();
        this.contentType = "application/zip";
        return;
    }
    this.log.debug("file size: #0", item.getFileSize());
    this.log.debug("data bytes available: #0", (null != item.getData()));
    if (null != item.getData()) {
        this.document = item.getData();
        return;
    }
    File file = item.getFile();
    if (null != file) {
        this.log.debug("tmp file: #0", file.getAbsolutePath());
        this.document = FileUtils.readFileToByteArray(file);
    }
}

From source file:be.fedict.eid.dss.sp.servlet.UploadServlet.java

@Override
@SuppressWarnings("unchecked")
protected void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {

    LOG.debug("doPost");

    String fileName = null;//from   ww  w.  j  a v  a  2s .  co m
    String contentType;
    byte[] document = null;

    FileItemFactory factory = new DiskFileItemFactory();
    // Create a new file upload handler
    ServletFileUpload upload = new ServletFileUpload(factory);
    // Parse the request
    try {
        List<FileItem> items = upload.parseRequest(request);
        if (!items.isEmpty()) {
            fileName = items.get(0).getName();
            // contentType = items.get(0).getContentType();
            document = items.get(0).get();
        }
    } catch (FileUploadException e) {
        throw new ServletException(e);
    }

    String extension = FilenameUtils.getExtension(fileName).toLowerCase();
    contentType = supportedFileExtensions.get(extension);
    if (null == contentType) {
        /*
         * Unsupported content-type is converted to a ZIP container.
         */
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        ZipOutputStream zipOutputStream = new ZipOutputStream(outputStream);
        ZipEntry zipEntry = new ZipEntry(fileName);
        zipOutputStream.putNextEntry(zipEntry);
        IOUtils.write(document, zipOutputStream);
        zipOutputStream.close();
        fileName = FilenameUtils.getBaseName(fileName) + ".zip";
        document = outputStream.toByteArray();
        contentType = "application/zip";
    }

    LOG.debug("File name: " + fileName);
    LOG.debug("Content Type: " + contentType);

    String signatureRequest = new String(Base64.encode(document));

    request.getSession().setAttribute(DOCUMENT_SESSION_ATTRIBUTE, document);
    request.getSession().setAttribute("SignatureRequest", signatureRequest);
    request.getSession().setAttribute("ContentType", contentType);

    response.sendRedirect(request.getContextPath() + this.postPage);
}

From source file:com.genericworkflownodes.knime.nodes.io.outputfile.OutputFileNodeModel.java

/**
 * {@inheritDoc}/* w  w  w.  java2s.com*/
 */
@Override
protected void saveInternals(final File internDir, final ExecutionMonitor exec)
        throws IOException, CanceledExecutionException {
    ZipOutputStream out = new ZipOutputStream(new FileOutputStream(new File(internDir, "loadeddata")));
    ZipEntry entry = new ZipEntry("rawdata.bin");
    out.putNextEntry(entry);
    out.write(data.getBytes());
    out.close();
}

From source file:free.yhc.netmbuddy.share.ExporterPlaylist.java

@Override
public Err execute() {
    JSONObject jsonPl = Json.playlistToJson(_mPlid);
    JSONObject jsonMeta = Json.createMetaJson(Type.PLAYLIST);
    JSONObject jo = new JSONObject();

    if (null == jsonPl)
        return Err.PARAMETER;

    eAssert(null != jsonMeta);/*from   w  ww  .  j  a va2  s  .  c  o m*/
    try {
        jo.put(Json.FMETA, jsonMeta);
        jo.put(Json.FPLAYLIST, jsonPl);
    } catch (JSONException e) {
        return Err.UNKNOWN;
    }

    String shareName = "";
    try {
        shareName = FileUtils.pathNameEscapeString(Utils.getResString(R.string.playlist) + "_"
                + jsonPl.getString(Json.FTITLE) + "." + Policy.SHARE_FILE_EXTENTION);
    } catch (JSONException e) {
        return Err.UNKNOWN;
    }

    ZipOutputStream zos;
    try {
        zos = new ZipOutputStream(new FileOutputStream(_mFout));
    } catch (FileNotFoundException e) {
        return Err.IO_FILE;
    }

    Err err = exportShareJson(zos, jo, shareName);
    try {
        zos.close();
    } catch (IOException e) {
        return Err.IO_FILE;
    }

    return err;
}

From source file:com.commonsware.android.backup.BackupService.java

private File buildBackup() throws IOException {
    File zipFile = new File(getCacheDir(), BACKUP_FILENAME);

    if (zipFile.exists()) {
        zipFile.delete();/*from w ww.  j a va  2  s . c  o m*/
    }

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

    zipDir(ZIP_PREFIX_FILES, getFilesDir(), zos);
    zipDir(ZIP_PREFIX_PREFS, getSharedPrefsDir(this), zos);
    zipDir(ZIP_PREFIX_EXTERNAL, getExternalFilesDir(null), zos);
    zos.flush();
    fos.getFD().sync();
    zos.close();

    return (zipFile);
}

From source file:es.urjc.mctwp.bbeans.research.image.DownloadBean.java

public String accDownloadStudyImages() {
    Study study = getSession().getStudy();

    try {/*from  w w w  .  j a  v a2  s.c  o  m*/
        if (study != null) {
            ZipOutputStream zos = prepareZOS(studyHeader + study.getDescription());
            downloadStudyImages(study, zos, null);
            if (zos != null)
                zos.close();
            completeResponse();
        }
    } catch (IOException e) {
        setErrorMessage(e.getLocalizedMessage());
        e.printStackTrace();
    }

    return null;
}