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

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


    List<String> fileNames = CollectUtils.newArrayList();
    String dest = destination;
    if (!destination.endsWith(File.separator)) {
      dest = destination + File.separator;
    }
    ZipFile file;
    try {
      file = null;
      if (null == encoding) file = new ZipFile(zipFile);
      else file = new ZipFile(zipFile, encoding);
      @SuppressWarnings("unchecked")
      Enumeration<ZipArchiveEntry> en = file.getEntries();
      ZipArchiveEntry ze = null;
      while (en.hasMoreElements()) {
        ze = en.nextElement();
        File f = new File(dest, ze.getName());
        if (ze.isDirectory()) {
          f.mkdirs();
          continue;
        } else {
          f.getParentFile().mkdirs();
          InputStream is = file.getInputStream(ze);
          OutputStream os = new FileOutputStream(f);
          IOUtils.copy(is, os);
          is.close();
          os.close();
          fileNames.add(f.getAbsolutePath());
        }
      }
      file.close();
    } catch (IOException e) {
      e.printStackTrace();
    }
    return fileNames;
  }
View Full Code Here


    return fileNames;
  }

  public static boolean isZipFile(File zipFile) {
    try {
      ZipFile zf = new ZipFile(zipFile);
      boolean isZip = zf.getEntries().hasMoreElements();
      zf.close();
      return isZip;
    } catch (IOException e) {
      return false;
    }
  }
View Full Code Here

          containerDir = new File(zipArchive.getParentFile(), fileName);
          containerDir.mkdir();
        } else {
          throw new IOException("Not a zipfile!");
        }
        ZipFile zipFile = new ZipFile(zipArchive);
      Enumeration<ZipArchiveEntry> entries = zipFile.getEntriesInPhysicalOrder();
     
      while (entries.hasMoreElements()) {
        ZipArchiveEntry currentEntry = entries.nextElement();
        String entryName = currentEntry.getName();
        logger.debug("Current entry: " + entryName);
        try {
          logger.debug(zipArchive.getParent() + "/" + currentEntry.getName());
          File destFile = new File(containerDir, currentEntry.getName());
          if (currentEntry.isDirectory()){
            destFile.mkdir();
            logger.debug("created directory: " + destFile.getAbsolutePath());
            // create the parent directory structure if needed
          } else {
            File parentDir = destFile.getParentFile();
            if (!parentDir.exists()){
              parentDir.mkdir();
            }
            destFile.createNewFile();
            logger.debug("created file: " + destFile.getAbsolutePath());
            copyInputStream(zipFile.getInputStream(currentEntry),
                new BufferedOutputStream(new FileOutputStream(destFile)));
            unarchivedFiles.add(destFile);
            logger.info("Unzipped file : " + destFile.getName());
          }
        } catch (Exception e){
          e.printStackTrace();
          logger.error("zip exception:" + e.getMessage());
          break;
        }
      }
     
      zipFile.close();
     
  } catch (FileNotFoundException e) {
    logger.error("file not found exception");
    //e.printStackTrace();
  } catch (IOException e) {
View Full Code Here

        }
    }

    private static MediaType detectZipFormat(TikaInputStream tis) {
        try {
            ZipFile zip = new ZipFile(tis.getFile()); // TODO: hasFile()?
            try {
                MediaType type = detectOpenDocument(zip);
                if (type == null) {
                    type = detectOfficeOpenXML(zip, tis);
                }
                if (type == null) {
                    type = detectIWork(zip);
                }
                if (type == null) {
                    type = detectJar(zip);
                }
                if (type == null) {
                    type = detectKmz(zip);
                }
                if (type == null) {
                    type = detectIpa(zip);
                }
                if (type != null) {
                    return type;
                }
            } finally {
                // TODO: shouldn't we record the open
                // container so it can be later
                // reused...?
                // tis.setOpenContainer(zip);
                try {
                    zip.close();
                } catch (IOException e) {
                    // ignore
                }
            }
        } catch (IOException e) {
View Full Code Here

    }

    // overridden in order to take advantage of ZipFile
    protected void expandFile(FileUtils fileUtils, File srcF, File dir) {
        log("Expanding: " + srcF + " into " + dir, Project.MSG_INFO);
        ZipFile zf = null;
        FileNameMapper mapper = getMapper();
        if (!srcF.exists()) {
            throw new BuildException("Unable to expand " + srcF
                                     + " as the file does not exist",
                                     getLocation());
        }
        try {
            zf = new ZipFile(srcF, getEncoding(), true);
            boolean empty = true;
            Enumeration e = zf.getEntries();
            while (e.hasMoreElements()) {
                empty = false;
                ZipArchiveEntry ze = (ZipArchiveEntry) e.nextElement();
                if (getSkipUnreadableEntries() && !zf.canReadEntryData(ze)) {
                    log(Messages.skippedIsUnreadable(ze));
                    continue;
                }
                log("extracting " + ze.getName(), Project.MSG_DEBUG);
                InputStream is = null;
                try {
                    extractFile(fileUtils, srcF, dir,
                                is = zf.getInputStream(ze),
                                ze.getName(), new Date(ze.getTime()),
                                ze.isDirectory(), mapper);
                } finally {
                    FileUtils.close(is);
                }
View Full Code Here

            return;
        }

        File srcFile = fp.getFile();
        ZipArchiveEntry entry = null;
        ZipFile zf = null;

        try {
            try {
                zf = new ZipFile(srcFile, encoding);
            } catch (ZipException ex) {
                throw new BuildException("Problem reading " + srcFile, ex);
            } catch (IOException ex) {
                throw new BuildException("Problem opening " + srcFile, ex);
            }
            Enumeration e = zf.getEntries();
            while (e.hasMoreElements()) {
                entry = (ZipArchiveEntry) e.nextElement();
                if (getSkipUnreadableEntries() && !zf.canReadEntryData(entry)) {
                    log(Messages.skippedIsUnreadable(entry));
                    continue;
                }
                Resource r = new ZipResource(srcFile, encoding, entry);
                String name = entry.getName();
View Full Code Here

   */
  @Test
  public void getLocalesForPackage() throws IOException{
    File widget = new File("parser/java/src-test/resources/localetest.wgt");
    assert widget.exists();
    ZipFile zipFile = new ZipFile(widget);
    String[] localesFromZip = WidgetPackageUtils.getLocalesFromZipFile(zipFile);   
    assertEquals(3, localesFromZip.length);
    assertEquals("en", localesFromZip[0]);
    assertEquals("en-gb-yorks", localesFromZip[1]);
    assertEquals("fr", localesFromZip[2]);
View Full Code Here

   * @throws BadWidgetZipFileException
   * @throws BadManifestException
   */
  private W3CWidget processWidgetPackage(File zipFile, String defaultIdentifier) throws BadWidgetZipFileException,
      BadManifestException, InsecuredWidgetContentException {
    ZipFile zip;
    try {
      zip = new ZipFile(zipFile);
    } catch (IOException e) {
      throw new BadWidgetZipFileException();
    }
    if (WidgetPackageUtils.hasManifest(zip)){
      try {
        // build the model
        WidgetManifestModel widgetModel = new WidgetManifestModel(WidgetPackageUtils.extractManifest(zip), locales, features, encodings, zip, defaultIdentifier);                             

        // get the widget identifier
        String manifestIdentifier = widgetModel.getIdentifier();           
        // create the folder structure to unzip the zip into
        unzippedWidgetDirectory = WidgetPackageUtils.createUnpackedWidgetFolder(outputDirectory, manifestIdentifier);
        // now unzip it into that folder
        WidgetPackageUtils.unpackZip(zip, unzippedWidgetDirectory);
        // checks for validity of widget using digital signatures
        if (digitalSignatureParser != null) {
          digitalSignatureParser
              .processDigitalSignatures(unzippedWidgetDirectory
                  .getAbsolutePath());
        }
        // Iterate over all start files and update paths
        for (IContent content: widgetModel.getContentList()){
          // now update the js links in the start page
          File startFile = new File(unzippedWidgetDirectory.getCanonicalPath() + File.separator + content.getSrc());
          String relativestartUrl = (WidgetPackageUtils.getURLForWidget(localPath, manifestIdentifier, content.getSrc()));          
          content.setSrc(relativestartUrl);
          if(startFile.exists() && startPageProcessor != null){   
            startPageProcessor.processStartFile(startFile, widgetModel, content);
          }
        }
        if (widgetModel.getContentList().isEmpty()){
          throw new InvalidStartFileException("Widget has no start page");
        }
       
        // get the path to the root of the unzipped folder
        String thelocalPath = WidgetPackageUtils.getURLForWidget(localPath, manifestIdentifier, "");
        // now pass this to the model which will prepend the path to local resources (not web icons)
        widgetModel.updateIconPaths(thelocalPath);
       
        // check to see if this widget already exists in the DB - using the ID (guid) key from the manifest
        return widgetModel;

      }catch(InsecuredWidgetContentException ex){
        throw ex;
      }catch (InvalidStartFileException e) {
        throw e;
      } catch (BadManifestException e) {
        throw e;
      } catch (Exception e){
        throw new BadManifestException(e);
      } finally
          try {
            zip.close();
          } catch (IOException e) {
            _logger.error("Unable to close wgt file:" + e.getMessage());
          }
      }
     
    }
    else{
        try {
                zip.close();
            } catch (IOException e) {
                _logger.error("Unable to close wgt file (without manifest):" + e.getMessage());
            }
      // no manifest file found in zip archive
      throw new BadWidgetZipFileException(); //$NON-NLS-1$
View Full Code Here

        String extension = FilenameUtils.getExtension(file);
        if(loadEntriesWithinZipArchives && (
                "zip".equalsIgnoreCase(extension) ||
                "jar".equalsIgnoreCase(extension))){
            log.info("  - processing {}-archive entries:",extension);
            ZipFile zipArchive;
            try {
                zipArchive = new ZipFile(file);
            } catch (IOException e) {
                zipArchive = null;
                setResourceState(file, ResourceState.ERROR,e);
            }
            if(zipArchive != null){
                boolean isError = false;
                Enumeration<ZipArchiveEntry> entries = zipArchive.getEntries();
                while(entries.hasMoreElements()){
                    ZipArchiveEntry entry = entries.nextElement();
                    if(!entry.isDirectory()){
                        String entryName = entry.getName();
                        log.info("     o loading entry '{}'", entryName);
                        try {
                            ResourceState state = resourceImporter.importResource(
                                zipArchive.getInputStream(entry),
                                FilenameUtils.getName(entryName));
                            if(state == ResourceState.ERROR){
                                isError = true;
                            }
                        } catch (IOException e) {
View Full Code Here

        }
    }

    private static MediaType detectZipFormat(TikaInputStream tis) {
        try {
            ZipFile zip = new ZipFile(tis.getFile()); // TODO: hasFile()?
            try {
                MediaType type = detectOpenDocument(zip);
                if (type == null) {
                    type = detectOfficeOpenXML(zip, tis);
                }
                if (type == null) {
                    type = detectIWork(zip);
                }
                if (type == null) {
                    type = detectJar(zip);
                }
                if (type == null) {
                    type = detectKmz(zip);
                }
                if (type != null) {
                    return type;
                }
            } finally {
                // TODO: shouldn't we record the open
                // container so it can be later
                // reused...?
                // tis.setOpenContainer(zip);
                try {
                    zip.close();
                } catch (IOException e) {
                    // ignore
                }
            }
        } catch (IOException e) {
View Full Code Here

TOP

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

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.