Package java.util.zip

Examples of java.util.zip.ZipOutputStream


    throws ServletException, IOException {

    try {
      response.setContentType("application/zip");

      ZipOutputStream zip =
        new ZipOutputStream(response.getOutputStream());

      // add resources
      Vector beans = Resources.getResourceXmlBeans(this);
      for (int i = 0; i < beans.size(); i++) {
        ResourceXmlBean bean = (ResourceXmlBean) beans.get(i);
        File root =
          new File(
            getServletContext().getRealPath("/"),
            resourcesPath);
        File dir = new File(root, bean.getDirectory());
        File[] files = dir.listFiles();
        for (int k = 0; k < files.length; k++) {
          File file = files[k];
          if (file.isFile()) {
            InputStream in = new FileInputStream(file);
            ZipEntry ze =
              new ZipEntry(
                "resources/"
                  + bean.getId()
                  + "/"
                  + file.getName());
            zip.putNextEntry(ze);

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

            zip.closeEntry();
            in.close();
          }
        }
      }

      // add images
      File root =
        new File(getServletContext().getRealPath("/"), imagesPath);
      File[] files = root.listFiles();
      for (int k = 0; k < files.length; k++) {
        File file = files[k];
        if (file.isFile()) {
          InputStream in = new FileInputStream(file);
          ZipEntry ze = new ZipEntry("images/" + file.getName());
          zip.putNextEntry(ze);

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

          zip.closeEntry();
          in.close();
        }
      }

      // add sections
      Mapping.begin();
      Vector sections = Section.listAll();
      Mapping.rollback();
      for (int i = 0; i < sections.size(); i++) {
        Section section = (Section) sections.get(i);
        try {
          URL url =
            new URL(
              frontUrl+"/section/"
                + section.getId()
                + ".html?static=true&nocache=true");
          BufferedReader reader =
            new BufferedReader(
              new InputStreamReader(url.openStream()));
          String content = "<!-- �ON static export -->";
          String line = "";
          while (line != null) {
            line = reader.readLine();
            if (line != null) {
              content += line;
            }
          }

          ZipEntry ze =
            new ZipEntry("section/" + section.getId() + ".html");
          zip.putNextEntry(ze);
          zip.write(content.getBytes());
          zip.closeEntry();
        } catch (Exception e) {
          System.out.println(
            "Static export error, section " + section.getId());
        }
      }

      // add publications
      Mapping.begin();
      Vector publications = Publication.listAll();
      Mapping.rollback();
      for (int i = 0; i < publications.size(); i++) {
        Publication publication = (Publication) publications.get(i);
        try {
          URL url =
            new URL(
              frontUrl+"/publication/"
                + publication.getId()
                + ".html?static=true&nocache=true");
          BufferedReader reader =
            new BufferedReader(
              new InputStreamReader(url.openStream()));
          String content = "<!-- �ON static export -->";
          String line = "";
          while (line != null) {
            line = reader.readLine();
            if (line != null) {
              content += line;
            }
          }
          ZipEntry ze =
            new ZipEntry(
              "publication/" + publication.getId() + ".html");
          zip.putNextEntry(ze);
          zip.write(content.getBytes());
          zip.closeEntry();
        } catch (Exception e) {
          System.out.println(
            "Static export error, publication "
              + publication.getId());
        }
      }

      // add index
      URL url =
        new URL(
          frontUrl+"?static=true&nocache=true");
      BufferedReader reader =
        new BufferedReader(new InputStreamReader(url.openStream()));
      String content = "<!-- �ON static export -->";
      String line = "";
      while (line != null) {
        line = reader.readLine();
        if (line != null) {
          content += line;
        }
      }
      ZipEntry ze = new ZipEntry("section/index.html");
      zip.putNextEntry(ze);
      zip.write(content.getBytes());
      zip.closeEntry();
      ze = new ZipEntry("index.html");
      zip.putNextEntry(ze);
      String redirect =
        "<META HTTP-EQUIV=\"Refresh\" CONTENT=\"0;URL=section/index.html\">";
      zip.write(redirect.getBytes());
      zip.closeEntry();

      zip.close();
    } catch (Exception e) {
      throw new ServletException(e);
    }

    response.getOutputStream().flush();
View Full Code Here


          manifest.delete();
        }
        transformConfig(config, "/vdb.xsl", new StreamResult(new File(metainf, "vdb.xml")));
        config.delete();
        FileOutputStream out = new FileOutputStream(new File(file.getParent(), fileName + "_70.vdb"));
        ZipOutputStream zos = new ZipOutputStream(new BufferedOutputStream(out));
        int parentLength = dir.getPath().length();
        addDirectory(dir, zos, parentLength);
        zos.close();
      } finally {
        FileUtils.removeDirectoryAndChildren(dir);
      }
    } else if (ext.endsWith("xml") || ext.endsWith("def")){
      File parent = file.getParentFile();
View Full Code Here

        writeDirectoryInfo(doc, filename);
      }
 
      // write it to the file
      OutputStream fs = new BufferedOutputStream(new FileOutputStream(f));
      ZipOutputStream zos = new ZipOutputStream(fs);
      zos.setLevel(9);
     
      try {
  //      writeContentToZipFile(doc, zos);
        storeContent(doc);
        writeHeadersToZipFile(doc, zos);
        writeUrlToZipFile(doc, zos);
        if (links != null) {
          writeLinksToZipFile(links, zos);
        }
      } catch (Throwable e){
        System.out.println(e);
      } finally {
        zos.close();
        fs.close();
        long date = doc.getDateAsMilliSeconds();
        f.setLastModified(date > 0 ? date : System.currentTimeMillis());
      }
    } catch (IOException ioex) {
View Full Code Here

    // write content
    File fzip = contentFile(md5, ".zip");
    if (!fzip.exists()) {
      checkStoragePathFor(CONTENT, useFirstCharactersAsDirectories(md5));
      OutputStream fs = new BufferedOutputStream(new FileOutputStream(fzip));
      ZipOutputStream zos = null;
      try {
        zos = new ZipOutputStream(fs);
        zos.setLevel(9);
        writeContentToZipFile(doc, zos);
      } finally {
        if (zos != null) {
          zos.close();
        } else {
          fs.close();
        }
      }
    } else {
View Full Code Here

          furnitureResourcesRemoteRelativeUrlBase = "";
        }
      }
    }
   
    ZipOutputStream zipOut = null;
    Map<Content, String> contentEntries = new HashMap<Content, String>();
    File furnitureLibraryFile = new File(furnitureLibraryName);
    File tmpFile = null;
    try {
      tmpFile = File.createTempFile("temp", ".sh3f");
      OutputStream out = new FileOutputStream(tmpFile);
      if (out != null) {
        // Create a zip output on file 
        zipOut = new ZipOutputStream(out);
        // Write furniture description file in first entry
        zipOut.putNextEntry(new ZipEntry(DefaultFurnitureCatalog.PLUGIN_FURNITURE_CATALOG_FAMILY + ".properties"));
        writeFurnitureLibraryProperties(zipOut, furnitureLibrary, furnitureLibraryFile,
            offlineFurnitureLibrary, contentMatchingFurnitureName,
            furnitureResourcesRemoteAbsoluteUrlBase, furnitureResourcesRemoteRelativeUrlBase,
            contentEntries);
        zipOut.closeEntry();
        // Write supported languages description files
        for (String language : furnitureLibrary.getSupportedLanguages()) {
          if (!FurnitureLibrary.DEFAULT_LANGUAGE.equals(language)) {
            zipOut.putNextEntry(new ZipEntry(DefaultFurnitureCatalog.PLUGIN_FURNITURE_CATALOG_FAMILY + "_" + language + ".properties"));
            writeFurnitureLibraryLocalizedProperties(zipOut, furnitureLibrary, language);
            zipOut.closeEntry();
          }
        }       
        // Write Content objects in files
        writeContents(zipOut, offlineFurnitureLibrary, furnitureResourcesLocalDirectory, contentEntries);
        // Finish zip writing
        zipOut.finish();
        zipOut.close();
        zipOut = null;

        copyFile(tmpFile, furnitureLibraryFile);
        tmpFile.delete();
      }
    } catch (IOException ex) {
      throw new RecorderException("Can't save furniture library file " + furnitureLibraryName, ex);
    } finally {
      if (zipOut != null) {
        try {
          zipOut.close();
        } catch (IOException ex) {
          throw new RecorderException("Can't close furniture library file " + furnitureLibraryName, ex);
        }
      }
    }
View Full Code Here

      // Write content entries in a separate zipped file, if the file doesn't exist
      File file = new File(furnitureResourcesLocalDirectory, mainEntryDirectory + ".zip");
      if (file.exists()) {
        return;
      }
      zipOut = new ZipOutputStream(new FileOutputStream(file));
      mainEntryDirectory = "";
    } else {
      mainEntryDirectory += "/";
    }
   
View Full Code Here

        try {
            if (new File(destFile).isDirectory()) {
                throw new IOException("Can't create the file as a directory with this name already exists: " + destFile);
            }
            OutputStream out = new BufferedOutputStream(new FileOutputStream(destFile));
            ZipOutputStream zipOut;
            if (jar) {
                zipOut = new JarOutputStream(out);
            } else {
                zipOut = new ZipOutputStream(out);
            }
            if (storeOnly) {
                zipOut.setMethod(ZipOutputStream.STORED);
            }
            zipOut.setLevel(Deflater.BEST_COMPRESSION);
            for (File file : files) {
                String fileName = file.getPath();
                String entryName = removeBase(basePath, fileName);
                byte[] data = readFile(file);
                ZipEntry entry = new ZipEntry(entryName);
                CRC32 crc = new CRC32();
                crc.update(data);
                entry.setSize(file.length());
                entry.setCrc(crc.getValue());
                zipOut.putNextEntry(entry);
                zipOut.write(data);
                zipOut.closeEntry();
            }
            zipOut.closeEntry();
            zipOut.close();
            return new File(destFile).length() / 1024;
        } catch (IOException e) {
            throw new RuntimeException("Error creating file " + destFile, e);
        }
    }
View Full Code Here

            IOUtils.delete(zipFileName);
        }
        OutputStream fileOut = null;
        try {
            fileOut = IOUtils.openFileOutputStream(zipFileName, false);
            ZipOutputStream zipOut = new ZipOutputStream(fileOut);
            for (String fileName : source) {
                String f = IOUtils.getCanonicalPath(fileName);
                if (!f.startsWith(base)) {
                    DbException.throwInternalError(f + " does not start with " + base);
                }
                if (f.endsWith(zipFileName)) {
                    continue;
                }
                if (IOUtils.isDirectory(fileName)) {
                    continue;
                }
                f = f.substring(base.length());
                f = BackupCommand.correctFileName(f);
                ZipEntry entry = new ZipEntry(f);
                zipOut.putNextEntry(entry);
                InputStream in = null;
                try {
                    in = IOUtils.openFileInputStream(fileName);
                    IOUtils.copyAndCloseInput(in, zipOut);
                } catch (FileNotFoundException e) {
                    // the file could have been deleted in the meantime
                    // ignore this (in this case an empty file is created)
                } finally {
                    IOUtils.closeSilently(in);
                }
                zipOut.closeEntry();
            }
            zipOut.closeEntry();
            zipOut.close();
        } catch (IOException e) {
            throw DbException.convertIOException(e, zipFileName);
        } finally {
            IOUtils.closeSilently(fileOut);
        }
View Full Code Here

    File file = new File(zipName);
    if (file.exists()) {
      ZipFile zipFile = new ZipFile(file.getAbsolutePath());
      Enumeration zipEntries = zipFile.entries();
      // create your output zip file
      ZipOutputStream newZip = new ZipOutputStream(
        new FileOutputStream(new File(file.getAbsolutePath() + "_TMP")));

      // Get all data (except the oldFileName) from zip file and
      // write it to the tmp zip file
      while (zipEntries.hasMoreElements()) {
        entry = (ZipEntry) zipEntries.nextElement();
        if (entry.getName().equalsIgnoreCase(oldFileName))
          continue;
        newZip.putNextEntry(new ZipEntry(entry.getName()));
        InputStream is = zipFile.getInputStream(entry);
        try {
          dump(is,newZip);
        } finally {
          newZip.closeEntry();
          is.close();
        }
      }
      zipFile.close();

      // Write the new fileName to the zip
      entry = new ZipEntry(path);
      newZip.putNextEntry(entry);
      try {
        FileInputStream fis = new FileInputStream(newFileName);
        dump(fis,newZip);
        fis.close();
      } catch (Exception exc) {
        System.out.println("Error reading input file: " + newFileName + " " + exc);
        newZip.close();
        new File(file.getAbsolutePath() + "_TMP").delete();
        throw exc;
      } finally {
        newZip.flush();
        newZip.closeEntry();
        newZip.finish();
        newZip.close();
      }
      String toRename = file.getAbsolutePath() + "_TMP";
      new File(file.getAbsolutePath()).delete();
      new File(toRename).renameTo(file);
    }
View Full Code Here

            out.close();
        } else {
            OutputStream out = null;
            try {
                out = new FileOutputStream(zipFile);
                ZipOutputStream zipOut = new ZipOutputStream(out);
                String baseName = base.getAbsolutePath();
                for (File f : list) {
                    String fileName = f.getAbsolutePath();
                    String entryName = fileName;
                    if (fileName.startsWith(baseName)) {
                        entryName = entryName.substring(baseName.length());
                    }
                    if (entryName.startsWith("\\")) {
                        entryName = entryName.substring(1);
                    }
                    if (!entryName.startsWith("/")) {
                        entryName = "/" + entryName;
                    }
                    ZipEntry entry = new ZipEntry(basePath + entryName);
                    zipOut.putNextEntry(entry);
                    InputStream in = null;
                    try {
                        in = new FileInputStream(fileName);
                        IOUtils.copyAndCloseInput(in, zipOut);
                    } finally {
                        IOUtils.closeSilently(in);
                    }
                    zipOut.closeEntry();
                }
                zipOut.closeEntry();
                zipOut.close();
            } finally {
                IOUtils.closeSilently(out);
            }
        }
        return zipFile;
View Full Code Here

TOP

Related Classes of java.util.zip.ZipOutputStream

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.