Package org.apache.commons.compress.archivers.zip

Examples of org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream


  // 文件压缩
  public static File zip(List<String> fileNames, String zipPath, String encoding) {
    try {
      FileOutputStream f = new FileOutputStream(zipPath);
      ZipArchiveOutputStream zos = (ZipArchiveOutputStream) new ArchiveStreamFactory()
          .createArchiveOutputStream(ArchiveStreamFactory.ZIP, f);
      if (null != encoding) {
        zos.setEncoding(encoding);
      }
      for (int i = 0; i < fileNames.size(); i++) {
        String fileName = fileNames.get(i);
        String entryName = StringUtils.substringAfterLast(fileName, File.separator);
        ZipArchiveEntry entry = new ZipArchiveEntry(entryName);
        zos.putArchiveEntry(entry);
        FileInputStream fis = new FileInputStream(fileName);
        IOUtils.copy(fis, zos);
        fis.close();
        zos.closeArchiveEntry();
      }
      zos.close();
      return new File(zipPath);
    } catch (Exception e) {
      throw new RuntimeException(e);
    }
  }
View Full Code Here


  final static Logger logger = LoggerFactory.getLogger(ZipFilePackager.class.getName());

  public static File zipUpFile(File fileToZip) throws IOException{
    String zipFileName = fileToZip.getName() + ".zip";
    File zipFile = new File(fileToZip.getParent(), zipFileName);
    ZipArchiveOutputStream newZipStream = new ZipArchiveOutputStream(zipFile);
    //add this uncompressed file to the archive
    int bytesRead;
    byte[] buffer = new byte[1024 * 1024];

    ZipArchiveEntry zipEntry = new ZipArchiveEntry(fileToZip.getName());
    newZipStream.putArchiveEntry(zipEntry);
    FileInputStream currentFileStream = new FileInputStream(fileToZip);
    while ((bytesRead = currentFileStream.read(buffer))!= -1) {
      newZipStream.write(buffer, 0, bytesRead);
    }
    newZipStream.closeArchiveEntry();
    newZipStream.close();
    currentFileStream.close();
    logger.info("Deleting: " + fileToZip.getName());
    fileToZip.delete();
    return zipFile;
  }
View Full Code Here

          return;
        }
      }
    byte[] buffer = new byte[1024 * 1024];
     
    ZipArchiveOutputStream newZipStream = new ZipArchiveOutputStream(zipArchive);
      int zipFileCounter = 0;
      for (File currentFile : filesToPackage){
        try{
          FileInputStream currentFileStream = new FileInputStream(currentFile);
          zipFileCounter++;
          if (!currentFile.getName().toLowerCase().endsWith(".zip")){
            logger.debug("Adding uncompressed file...");
            //add this uncompressed file to the archive
            int bytesRead;
            String entryName = currentFile.getName();
            logger.debug("Zipping: " + entryName);
            ZipArchiveEntry zipEntry = new ZipArchiveEntry(entryName);
            newZipStream.putArchiveEntry(zipEntry);
            while ((bytesRead = currentFileStream.read(buffer))!= -1) {
              newZipStream.write(buffer, 0, bytesRead);
            }
            newZipStream.closeArchiveEntry();
          } else {
            logger.debug("Adding entries from compressed file...");
            //read the entries from the zip file and copy them to the new zip archive
            //so that we don't have to recompress them.
            ZipArchiveInputStream currentZipStream = new ZipArchiveInputStream(currentFileStream);
            ArchiveEntry currentEntry;
            while ((currentEntry = currentZipStream.getNextEntry()) != null) {
              String entryName = currentEntry.getName();
              logger.debug("Zipping: " + entryName);
              ZipArchiveEntry zipEntry = new ZipArchiveEntry(entryName);
              try {
                newZipStream.putArchiveEntry(zipEntry);
              } catch (Exception e){
                //duplicate names should never happen.
                entryName = Math.round(Math.random() * 10000) + "_" + entryName;
                ZipArchiveEntry zipEntry2 = new ZipArchiveEntry(entryName);
                newZipStream.putArchiveEntry(zipEntry2);
              }
              int bytesRead;
              while ((bytesRead = currentZipStream.read(buffer))!= -1) {
                newZipStream.write(buffer, 0, bytesRead);
                    }
              newZipStream.closeArchiveEntry();
            }
            currentZipStream.close();
         
        } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
      } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
      } finally {
        //always delete the file
        logger.debug("Deleting: " + currentFile.getName());
          currentFile.delete();

        }
      }
     
      if (zipFileCounter > 0){
         try {
        newZipStream.close();
      } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
      }
      }
View Full Code Here

    public Zip() {
        setFactory(new ZipStreamFactory() {
                public ArchiveOutputStream getArchiveStream(OutputStream stream,
                                                            String encoding)
                    throws IOException {
                    ZipArchiveOutputStream o =
                        (ZipArchiveOutputStream) super.getArchiveStream(stream,
                                                                        encoding);
                    configure(o);
                    return o;
                }
                public ArchiveOutputStream getArchiveOutputStream(File f,
                                                                  String encoding)
                    throws IOException {
                    ZipArchiveOutputStream o = (ZipArchiveOutputStream)
                        super.getArchiveOutputStream(f, encoding);
                    configure(o);
                    return o;
                }
            });
View Full Code Here

     * @param encoding the encoding of the entry names
     */
    public ArchiveOutputStream getArchiveStream(OutputStream stream,
                                                String encoding)
        throws IOException {
        ZipArchiveOutputStream o = new ZipArchiveOutputStream(stream);
        o.setEncoding(encoding);
        return o;
    }
View Full Code Here

     * @param encoding the encoding of the entry names
     */
    public ArchiveOutputStream getArchiveOutputStream(File file,
                                                      String encoding)
        throws IOException {
        ZipArchiveOutputStream o = new ZipArchiveOutputStream(file);
        o.setEncoding(encoding);
        return o;
    }
View Full Code Here

    }

    /** Pack the content into a .wgt package */
    public void repackZip() throws IOException {
        File sourceOfFiles = new File(pathToWidget);
        ZipArchiveOutputStream out = new ZipArchiveOutputStream(new File(
            sourceOfFiles.getAbsolutePath() + File.separator + name + ".wgt"));
        out.setEncoding("UTF-8");
        for(File aFile : sourceOfFiles.listFiles()){
            if(!aFile.getName().endsWith(".wgt")){
                pack(aFile, out, "");
            }
        }
        out.flush();
        out.close();
    }
View Full Code Here

   * @param source the source file or folder to be zipped
   * @param target the zip file to create
   * @throws IOException
   */
  public static void repackZip(File source, File target) throws IOException{
    ZipArchiveOutputStream out = new ZipArchiveOutputStream(target);
    out.setEncoding("UTF-8");
        for(File afile: source.listFiles()){
            pack(afile,out, "");
        }
    out.flush();
    out.close();
  }
View Full Code Here

   * @param source the source file or folder to be zipped
   * @param target the zip file to create
   * @throws IOException
   */
  public static void repackZip(File source, File target) throws IOException{
    ZipArchiveOutputStream out = new ZipArchiveOutputStream(target);
    out.setEncoding("UTF-8");
        for(File afile: source.listFiles()){
            pack(afile,out, "");
        }
    out.flush();
    out.close();
  }
View Full Code Here

    public Zip() {
        setFactory(new ZipStreamFactory() {
                public ArchiveOutputStream getArchiveStream(OutputStream stream,
                                                            String encoding)
                    throws IOException {
                    ZipArchiveOutputStream o =
                        (ZipArchiveOutputStream) super.getArchiveStream(stream,
                                                                        encoding);
                    o.setLevel(level);
                    o.setComment(comment);
                    o.setFallbackToUTF8(fallBackToUTF8);
                    o.setUseLanguageEncodingFlag(useLanguageEncodingFlag);
                    o.setCreateUnicodeExtraFields(createUnicodeExtraFields
                                                  .getPolicy());
                    return o;
                }
            });
        setEntryBuilder(
View Full Code Here

TOP

Related Classes of org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream

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.