/**
   * @param garFile GAR file.
   * @param hasDescr Whether GAR file has descriptor.
   * @throws IOException If GAR file is not found.
   * @return Whether GAR file structure is correct.
   */
  private boolean checkStructure(File garFile, boolean hasDescr) throws IOException {
    ZipFile zip = new ZipFile(garFile);

    String descr = "META-INF/ignite.xml";

    ZipEntry entry = zip.getEntry(descr);

    if (entry == null && !hasDescr) {
      if (log().isInfoEnabled()) {
        log()
            .info(
                "Process deployment without descriptor file [path="
                    + descr
                    + ", file="
                    + garFile.getAbsolutePath()
                    + ']');
      }

      return true;
    } else if (entry != null && hasDescr) {
      InputStream in = null;

      try {
        in = new BufferedInputStream(zip.getInputStream(entry));

        return true;
      } finally {
        U.close(in, log());
      }
    } else return false;
  }
예제 #2
0
  private boolean unpackFile(ZipFile zip, byte[] buf, ZipEntry fileEntry, String name)
      throws IOException, FileNotFoundException {
    if (fileEntry == null) fileEntry = zip.getEntry(name);
    if (fileEntry == null)
      throw new FileNotFoundException("Can't find " + name + " in " + zip.getName());

    File outFile = new File(sGREDir, name);
    if (outFile.lastModified() == fileEntry.getTime() && outFile.length() == fileEntry.getSize())
      return false;

    File dir = outFile.getParentFile();
    if (!dir.exists()) dir.mkdirs();

    InputStream fileStream;
    fileStream = zip.getInputStream(fileEntry);

    OutputStream outStream = new FileOutputStream(outFile);

    while (fileStream.available() > 0) {
      int read = fileStream.read(buf, 0, buf.length);
      outStream.write(buf, 0, read);
    }

    fileStream.close();
    outStream.close();
    outFile.setLastModified(fileEntry.getTime());
    return true;
  }
예제 #3
0
파일: FNZipTest.java 프로젝트: godmar/basex
  /**
   * Checks the contents of the specified zip entry.
   *
   * @param file file to be checked
   * @param data expected file contents
   * @throws IOException I/O exception
   */
  private static void checkEntry(final String file, final byte[] data) throws IOException {

    ZipFile zf = null;
    try {
      zf = new ZipFile(TMPZIP);
      final ZipEntry ze = zf.getEntry(file);
      assertNotNull("File not found: " + file, ze);
      final DataInputStream is = new DataInputStream(zf.getInputStream(ze));
      final byte[] dt = new byte[(int) ze.getSize()];
      is.readFully(dt);
      assertTrue(
          "Wrong contents in file \""
              + file
              + "\":"
              + Prop.NL
              + "Expected: "
              + string(data)
              + Prop.NL
              + "Found: "
              + string(dt),
          eq(data, dt));
    } finally {
      if (zf != null) zf.close();
    }
  }
예제 #4
0
  public static void main(String[] args) throws Exception {
    Reader trainingFile = null;

    // Process arguments
    int restArgs = commandOptions.processOptions(args);

    // Check arguments
    if (restArgs != args.length) {
      commandOptions.printUsage(true);
      throw new IllegalArgumentException("Unexpected arg " + args[restArgs]);
    }
    if (trainFileOption.value == null) {
      commandOptions.printUsage(true);
      throw new IllegalArgumentException("Expected --train-file FILE");
    }
    if (modelFileOption.value == null) {
      commandOptions.printUsage(true);
      throw new IllegalArgumentException("Expected --model-file FILE");
    }

    // Get the CRF structure specification.
    ZipFile zipFile = new ZipFile(modelFileOption.value);
    ZipEntry zipEntry = zipFile.getEntry("crf-info.xml");
    CRFInfo crfInfo = new CRFInfo(zipFile.getInputStream(zipEntry));

    StringBuffer crfInfoBuffer = new StringBuffer();
    BufferedReader reader =
        new BufferedReader(new InputStreamReader(zipFile.getInputStream(zipEntry)));
    String line;
    while ((line = reader.readLine()) != null) {
      crfInfoBuffer.append(line).append('\n');
    }
    reader.close();

    // Create the CRF, and train it.
    CRF4 crf = createCRF(trainFileOption.value, crfInfo);

    // Create a new zip file for our output.  This will overwrite
    // the file we used for input.
    ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(modelFileOption.value));

    // Copy the CRF info xml to the output zip file.
    zos.putNextEntry(new ZipEntry("crf-info.xml"));
    BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(zos));
    writer.write(crfInfoBuffer.toString());
    writer.flush();
    zos.closeEntry();

    // Save the CRF classifier model to the output zip file.
    zos.putNextEntry(new ZipEntry("crf-model.ser"));
    ObjectOutputStream oos = new ObjectOutputStream(zos);
    oos.writeObject(crf);
    oos.flush();
    zos.closeEntry();
    zos.close();
  }
예제 #5
0
  public void installFramework(File frameFile, String tag) throws AndrolibException {
    InputStream in = null;
    ZipOutputStream out = null;
    try {
      ZipFile zip = new ZipFile(frameFile);
      ZipEntry entry = zip.getEntry("resources.arsc");

      if (entry == null) {
        throw new AndrolibException("Can't find resources.arsc file");
      }

      in = zip.getInputStream(entry);
      byte[] data = IOUtils.toByteArray(in);

      ARSCData arsc = ARSCDecoder.decode(new ByteArrayInputStream(data), true, true);
      publicizeResources(data, arsc.getFlagsOffsets());

      File outFile =
          new File(
              getFrameworkDir(),
              String.valueOf(arsc.getOnePackage().getId())
                  + (tag == null ? "" : '-' + tag)
                  + ".apk");

      out = new ZipOutputStream(new FileOutputStream(outFile));
      out.setMethod(ZipOutputStream.STORED);
      CRC32 crc = new CRC32();
      crc.update(data);
      entry = new ZipEntry("resources.arsc");
      entry.setSize(data.length);
      entry.setCrc(crc.getValue());
      out.putNextEntry(entry);
      out.write(data);

      LOGGER.info("Framework installed to: " + outFile);
    } catch (ZipException ex) {
      throw new AndrolibException(ex);
    } catch (IOException ex) {
      throw new AndrolibException(ex);
    } finally {
      if (in != null) {
        try {
          in.close();
        } catch (IOException ex) {
        }
      }
      if (out != null) {
        try {
          out.close();
        } catch (IOException ex) {
        }
      }
    }
  }
예제 #6
0
  public static void main(String[] args) {
    try {
      ZipFile zipFile = new ZipFile(testDirName + zipFileName);
      ZipEntry ze = zipFile.getEntry(zipEntryName);
      byte[] digest1 = getZipDigest(zipFile, ze, true);
      System.out.println("Digest1: " + Base64Util.encode(digest1));
      byte[] digest2 = getZipDigest(zipFile, ze, false);
      System.out.println("Digest2: " + Base64Util.encode(digest2));

    } catch (Exception ex) {
      System.out.println("Error: " + ex.toString());
      ex.printStackTrace(System.out);
    }
  }
    /**
     * Get an input stream for a resource contained in the jar file
     *
     * @param name file name of the resource within the jar file
     * @return an input stream representing the resouce if it exists or null
     */
    public InputStream getResourceAsStream(String name) {

      if (zfile == null || hasChanged()) {
        update();
      }
      try {
        if (zfile == null) return null;
        ZipEntry e = zfile.getEntry(name);
        if (e != null) return zfile.getInputStream(e);
      } catch (Exception e) {
        if (verbose)
          System.err.println("RJavaClassLoader$UnixJarFile: exception: " + e.getMessage());
      }
      return null;
    }
    public URL getResource(String name) {
      if (zfile == null || zfile.getEntry(name) == null) {
        return null;
      }

      URL u = null;
      if (urlPrefix == null) {
        try {
          urlPrefix = "jar:" + toURL().toString() + "!";
        } catch (java.net.MalformedURLException ex) {
        } catch (java.io.IOException ex) {
        }
      }

      try {
        u = new URL(urlPrefix + name);
      } catch (java.net.MalformedURLException ex) {
        /* not to worry */
      }
      return u;
    }
 /** @see "https://issues.apache.org/jira/browse/COMPRESS-189" */
 @Test
 public void properUseOfInflater() throws Exception {
   ZipFile zf = null;
   ZipArchiveInputStream in = null;
   try {
     zf = new ZipFile(getFile("COMPRESS-189.zip"));
     ZipArchiveEntry zae = zf.getEntry("USD0558682-20080101.ZIP");
     in = new ZipArchiveInputStream(new BufferedInputStream(zf.getInputStream(zae)));
     ZipArchiveEntry innerEntry;
     while ((innerEntry = in.getNextZipEntry()) != null) {
       if (innerEntry.getName().endsWith("XML")) {
         assertTrue(0 < in.read());
       }
     }
   } finally {
     if (zf != null) {
       zf.close();
     }
     if (in != null) {
       in.close();
     }
   }
 }
 public ZipEntry getEntry(String name) {
   return zf.getEntry(name);
 }
예제 #11
0
 public InputStream getInputStream() throws IOException {
   ZipEntry entry = zip.getEntry(entryName);
   if (entry == null) throw new ZipException("Entry " + entryName + " not found in " + zip);
   InputStream in = zip.getInputStream(entry);
   return in;
 }