Example #1
0
 public static void main(final String[] args) throws IOException {
   File f = new File("pokus.zip");
   //    ZipFile zf = new ZipFile(f, ZipFile.OPEN_READ);
   //    ZipFile zf = new ZipFile(f, ZipFile.OPEN_READ | ZipFile.OPEN_DELETE);
   ZipFile zf = new ZipFile(f, ZipFile.OPEN_DELETE);
   System.out.println("pocet zapakovanych polozek: " + zf.size());
 }
  protected void load(String filename) {
    Logger.getLogger(getClass()).debug("Starting group in file " + filename);

    ZipFile zipfile = null;
    try {
      zipfile = new ZipFile(filename);

      fireBeginGroup(filename, zipfile.size());

      Logger.getLogger(getClass()).debug("Loading ZipFile " + filename);
      load(zipfile);
      Logger.getLogger(getClass()).debug("Loaded ZipFile " + filename);

      fireEndGroup(filename);
    } catch (IOException ex) {
      Logger.getLogger(getClass()).error("Cannot load Zip file \"" + filename + "\"", ex);
    } finally {
      if (zipfile != null) {
        try {
          zipfile.close();
        } catch (IOException ex) {
          // Ignore
        }
      }
    }
  }
  private DbJVPackage importJarFile(
      File jarFile,
      DbJVPackage topMostPackage,
      Controller controller,
      int startJobDone,
      int endJobDone)
      throws DbException {
    DbJVClass dbClass;
    String filename = jarFile.getName();

    try {
      ZipFile zip = new ZipFile(jarFile);
      int i = 0, nb = zip.size();
      int span = endJobDone - startJobDone;

      for (Enumeration<?> e = zip.entries(); e.hasMoreElements(); ) {
        int jobDone = startJobDone + (i * span) / nb;

        ZipEntry entry = (ZipEntry) e.nextElement();
        String entryName = entry.getName();
        int idx = entryName.lastIndexOf('.');
        String ext = (idx == -1) ? null : entryName.substring(idx + 1);
        if ("class".equals(ext)) {
          InputStream is = zip.getInputStream(entry);

          ClassParser parser = new ClassParser(is, filename);
          JavaClass claz = parser.parse();
          dbClass = importClass(claz, controller);

          if (dbClass != null) {
            DbJVPackage pack = (DbJVPackage) dbClass.getCompositeOfType(DbJVPackage.metaClass);
            topMostPackage = findTopMostPackage(topMostPackage, pack);
            addToImportedPackage(pack);
          } // end if
        } // end if

        // check job done
        controller.checkPoint(jobDone);
        i++;

        // stop to reverse engineer if user has cancelled
        boolean finished = controller.isFinalState();
        if (finished) {
          break;
        }
      } // end for

      zip.close();

    } catch (IOException ex) {
      controller.println(ex.toString());
      dbClass = null;
    }

    return topMostPackage;
  } // end importClassFile()
Example #4
0
  @Test
  public void testCloseQuietlyZipFileOpen() throws IOException {
    final ZipFile zf = new ZipFile("test/VASSAL/tools/io/test.zip");
    IOUtils.closeQuietly(zf);

    try {
      zf.size();
      fail();
    } catch (IllegalStateException e) {
      // This is the expected behavior of size().
    }
  }
Example #5
0
 private static int getItemCount(File file) throws IOException {
   ZipFile zip = null;
   int count = 0;
   try {
     zip = new ZipFile(file);
     count = zip.size();
   } finally {
     try {
       zip.close();
     } catch (Throwable e) {
       // ignore
     }
   }
   return count;
 }
  @NotNull
  @Override
  protected Map<String, EntryInfo> createEntriesMap() throws IOException {
    FileAccessorCache.Handle<ZipFile> zipRef = getZipFileHandle();
    try {
      ZipFile zip = zipRef.get();

      Map<String, EntryInfo> map = new ZipEntryMap(zip.size());
      map.put("", createRootEntry());

      Enumeration<? extends ZipEntry> entries = zip.entries();
      while (entries.hasMoreElements()) {
        getOrCreate(entries.nextElement(), map, zip);
      }

      return map;
    } finally {
      zipRef.release();
    }
  }
 // TODO: how can this be improved?
 private boolean isZipFile(File file) {
   if (file != null && file.exists() && file.getName().endsWith(".zip")) {
     ZipFile zf = null;
     try {
       zf = new ZipFile(file);
       return zf.size() > 0;
     } catch (ZipException e) {
       mLogger.error(e.getMessage());
     } catch (IOException e) {
       mLogger.error(e.getMessage());
     } finally {
       if (zf != null) {
         try {
           zf.close();
         } catch (IOException e) {
           mLogger.error(e.getMessage());
         }
       }
     }
   }
   return false;
 }
 public int size() {
   return zf.size();
 }
Example #9
0
  public void unzip(File zipFile, File dir) throws Exception {

    InputStream is = null;
    OutputStream os = null;
    ZipFile zip = null;

    try {

      zip = new ZipFile(zipFile);
      Enumeration<? extends ZipEntry> e = zip.entries();

      byte[] buffer = new byte[2048];
      int currentEntry = 0;

      while (e.hasMoreElements()) {

        ZipEntry zipEntry = (ZipEntry) e.nextElement();
        File file = new File(dir, zipEntry.getName());
        currentEntry++;

        if (zipEntry.isDirectory()) {

          if (!file.exists()) file.mkdirs();

          continue;
        }

        if (!file.getParentFile().exists()) file.getParentFile().mkdirs();

        try {

          int bytesLidos = 0;

          is = zip.getInputStream(zipEntry);
          os = new FileOutputStream(file);

          if (is == null)
            throw new ZipException("Erro ao ler a entrada do zip: " + zipEntry.getName());

          while ((bytesLidos = is.read(buffer)) > 0) os.write(buffer, 0, bytesLidos);

          if (listener != null) listener.unzip((100 * currentEntry) / zip.size());

        } finally {

          if (is != null)
            try {
              is.close();
            } catch (Exception ex) {
            }

          if (os != null)
            try {
              os.close();
            } catch (Exception ex) {
            }
        }
      }

    } finally {

      if (zip != null)
        try {
          zip.close();
        } catch (Exception e) {
        }
    }
  }
Example #10
0
 @Override
 public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
   Path relativePath = rootPath.relativize(file);
   String downloadURL =
       urlBase + "/" + relativePath.toString().replace("\\", "/").replace(" ", "%20");
   InputStream is = Files.newInputStream(file);
   byte[] hash = DigestUtils.md5(is);
   String md5 = new String(Hex.encodeHex(hash));
   String name = file.getFileName().toString();
   String id;
   name = name.substring(0, name.length() - 4);
   id = name.replace(" ", "");
   String depends = "";
   Boolean required = true;
   Boolean inJar = false;
   Boolean extract = false;
   Boolean inRoot = false;
   Boolean isDefault = true;
   Boolean coreMod = false;
   System.out.println(relativePath.toString());
   if (relativePath.toString().contains(".DS_Store")) {
     return FileVisitResult.CONTINUE;
   }
   if (relativePath.toString().indexOf(sep) >= 0) {
     switch (relativePath.toString().substring(0, relativePath.toString().indexOf(sep))) {
         // Ignore these folders
       case "bin":
       case "lib":
       case "resources":
       case "saves":
       case "screenshots":
       case "stats":
       case "texturepacks":
       case "texturepacks-mp-cache":
         return FileVisitResult.CONTINUE;
         //
       case "instMods":
       case "jar":
         {
           inJar = true;
           break;
         }
       case "coremods":
         {
           coreMod = true;
           break;
         }
       case "config":
         {
           String newPath = relativePath.toString();
           if (sep.equals("\\")) {
             newPath = newPath.replace("\\", "/");
           }
           ConfigFile newConfig = new ConfigFile(downloadURL, newPath, false, md5);
           parent.AddConfig(new ConfigFileWrapper("", newConfig));
           return FileVisitResult.CONTINUE;
         }
       default:
     }
   }
   try {
     ZipFile zf = new ZipFile(file.toFile());
     System.out.println(zf.size() + " entries in file.");
     JdomParser parser = new JdomParser();
     JsonRootNode modInfo =
         parser.parse(new InputStreamReader(zf.getInputStream(zf.getEntry("mcmod.info"))));
     JsonNode subnode;
     if (modInfo.hasElements()) {
       subnode = modInfo.getElements().get(0);
     } else {
       subnode = modInfo.getNode("modlist").getElements().get(0);
     }
     id = subnode.getStringValue("modid");
     name = subnode.getStringValue("name");
     zf.close();
   } catch (NullPointerException e) {
   } catch (ZipException e) {
     System.out.println("Not an archive.");
   } catch (IOException e) {
     e.printStackTrace();
   } catch (InvalidSyntaxException e) {
     e.printStackTrace();
   } finally {
     Module newMod =
         new Module(
             name,
             id,
             downloadURL,
             depends,
             required,
             inJar,
             0,
             true,
             extract,
             inRoot,
             isDefault,
             coreMod,
             md5,
             null,
             "both",
             null);
     parent.AddModule(newMod);
   }
   return FileVisitResult.CONTINUE;
 }
Example #11
0
    @Override
    protected Integer doInBackground(Void... param) {
      fApkDir.mkdirs();

      // Log.d(TAG, "apk: " + sPackagePath);
      // Log.d(TAG, "output: " + sApkDir);

      ZipFile zip;
      byte[] buf = new byte[4096];
      int n;
      try {
        zip = new ZipFile(sPackagePath);
        Enumeration<? extends ZipEntry> entries = zip.entries();
        mProgress.setProgress(0);
        mProgress.setMax(zip.size());

        mState = State.Caching;
        publishProgress(mProgressStatus);
        while (entries.hasMoreElements()) {
          // Update the progress bar
          publishProgress(++mProgressStatus);

          ZipEntry e = (ZipEntry) entries.nextElement();

          if (!e.getName().startsWith("assets/")) continue;
          if (e.getName().startsWith("assets/python2.6")) continue;

          String sFullPath = sApkDir + "/" + e.getName();
          File fFullPath = new File(sFullPath);
          if (e.isDirectory()) {
            // Log.d(TAG, "creating dir: " + sFullPath);
            fFullPath.mkdirs();
            continue;
          }

          // Log.d(TAG,
          // "time: " + e.getTime() + ";"
          // + fFullPath.lastModified());

          // If file exists and has same time, skip
          if (e.getTime() == fFullPath.lastModified()) continue;

          // Log.d(TAG, "writing: " + sFullPath);
          fFullPath.getParentFile().mkdirs();

          try {
            InputStream in = zip.getInputStream(e);
            BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(sFullPath));
            while ((n = in.read(buf, 0, 4096)) > -1) out.write(buf, 0, n);

            in.close();
            out.close();

            // save the zip time. this way we know for certain
            // if we
            // need to refresh.
            fFullPath.setLastModified(e.getTime());
          } catch (IOException e1) {
            e1.printStackTrace();
          }
        }

        zip.close();

        fApkDir.setLastModified(fPackagePath.lastModified());

      } catch (FileNotFoundException e1) {
        e1.printStackTrace();
        mErrorMsg = "Cannot find package.";
        return -1;
      } catch (IOException e) {
        e.printStackTrace();
        mErrorMsg = "Cannot read package.";
        return -1;
      }

      mState = State.StartingXBMC;
      publishProgress(0);

      return 0;
    }