/**
   * Loop through the ZIP file, copying its entries into a new, temporary ZIP file, skipping the
   * entry to be deleted. Then replace the original file with the new one.
   */
  protected Integer deleteEntry() throws XMLDataStoreException {
    try {
      ZipInputStream inStream = this.buildZipInputStream();
      File outFile = this.buildTempFile();
      ZipOutputStream outStream = new ZipOutputStream(new FileOutputStream(outFile));

      byte[] buffer = new byte[32768];
      int inCount = 0;
      int outCount = 0;

      // copy all the entries except the one to be deleted
      ZipEntry entry = inStream.getNextEntry();
      while (entry != null) {
        inCount++;
        if (!this.getZipEntryName().equals(entry.getName())) {
          outCount++;
          outStream.putNextEntry(entry);
          int byteCount;
          while ((byteCount = inStream.read(buffer)) != -1) {
            outStream.write(buffer, 0, byteCount);
          }
          outStream.closeEntry();
        }
        entry = inStream.getNextEntry();
      }
      inStream.close();
      if (outCount == 0) {
        // add a dummy record to an empty file so we can close it
        // this is required by ZipOutputStream
        outStream.putNextEntry(new ZipEntry("delete.txt"));
        outStream.write(
            "This file is a place-holder. The containing ZIP file should be deleted.".getBytes());
        outStream.closeEntry();
      }
      outStream.close();
      if (outCount == inCount) {
        // no entries were removed - just delete the temp file
        outFile.delete();
      } else {
        // at least one entry removed - delete the original file
        this.getFile().delete();
        if (outCount == 0) {
          // NO entries remain - just delete the temp file too
          outFile.delete();
        } else {
          // entries remain - replace original file with temp file
          outFile.renameTo(this.getFile());
        }
      }
      return new Integer(inCount - outCount); // should be 0 or 1
    } catch (IOException ex) {
      throw XMLDataStoreException.ioException(ex);
    }
  }
  /**
   * Puts the uninstaller.
   *
   * @exception Exception Description of the Exception
   */
  private void putUninstaller() throws Exception {
    // Me make the .uninstaller directory
    String dest = translatePath("$INSTALL_PATH") + File.separator + "Uninstaller";
    String jar = dest + File.separator + "uninstaller.jar";
    File pathMaker = new File(dest);
    pathMaker.mkdirs();

    // We log the uninstaller deletion information
    UninstallData udata = UninstallData.getInstance();
    udata.setUninstallerJarFilename(jar);
    udata.setUninstallerPath(dest);

    // We open our final jar file
    FileOutputStream out = new FileOutputStream(jar);
    ZipOutputStream outJar = new ZipOutputStream(out);
    idata.uninstallOutJar = outJar;
    outJar.setLevel(9);
    udata.addFile(jar);

    // We copy the uninstaller
    InputStream in = getClass().getResourceAsStream("/res/IzPack.uninstaller");
    ZipInputStream inRes = new ZipInputStream(in);
    ZipEntry zentry = inRes.getNextEntry();
    while (zentry != null) {
      // Puts a new entry
      outJar.putNextEntry(new ZipEntry(zentry.getName()));

      // Byte to byte copy
      int unc = inRes.read();
      while (unc != -1) {
        outJar.write(unc);
        unc = inRes.read();
      }

      // Next one please
      inRes.closeEntry();
      outJar.closeEntry();
      zentry = inRes.getNextEntry();
    }
    inRes.close();

    // We put the langpack
    in = getClass().getResourceAsStream("/langpacks/" + idata.localeISO3 + ".xml");
    outJar.putNextEntry(new ZipEntry("langpack.xml"));
    int read = in.read();
    while (read != -1) {
      outJar.write(read);
      read = in.read();
    }
    outJar.closeEntry();
  }
Ejemplo n.º 3
0
  public static String CreateZip(String[] filesToZip, String zipFileName) {

    byte[] buffer = new byte[18024];

    try {

      ZipOutputStream out = new ZipOutputStream(new FileOutputStream(zipFileName));
      out.setLevel(Deflater.BEST_COMPRESSION);
      for (int i = 0; i < filesToZip.length; i++) {
        FileInputStream in = new FileInputStream(filesToZip[i]);
        String fileName = null;
        for (int X = filesToZip[i].length() - 1; X >= 0; X--) {
          if (filesToZip[i].charAt(X) == '\\' || filesToZip[i].charAt(X) == '/') {
            fileName = filesToZip[i].substring(X + 1);
            break;
          } else if (X == 0) fileName = filesToZip[i];
        }
        out.putNextEntry(new ZipEntry(fileName));
        int len;
        while ((len = in.read(buffer)) > 0) out.write(buffer, 0, len);
        out.closeEntry();
        in.close();
      }
      out.close();
    } catch (IllegalArgumentException e) {
      return "Failed to create zip: " + e.toString();
    } catch (FileNotFoundException e) {
      return "Failed to create zip: " + e.toString();
    } catch (IOException e) {
      return "Failed to create zip: " + e.toString();
    }
    return "Success";
  }
Ejemplo n.º 4
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) {
        }
      }
    }
  }
  /**
   * Rename the ZIP file to a temporary file, then copy its entries back into the original ZIP file,
   * leaving the stream open and returning it.
   */
  protected Writer buildWriteStream(boolean entryShouldExist) throws XMLDataStoreException {
    try {
      File inFile = this.buildTempFile();
      inFile.delete(); // the file actually gets created - delete it
      boolean renameSucceeded = this.getFile().renameTo(inFile);
      ZipInputStream inStream = this.buildZipInputStream(inFile);
      ZipOutputStream outStream = this.buildZipOutputStream();

      boolean entryActuallyExists = false;
      byte[] buffer = new byte[32768];
      ZipEntry entry = inStream.getNextEntry();
      while (entry != null) {
        boolean weWantToWriteTheEntry = true;
        if (this.getZipEntryName().equals(entry.getName())) {
          entryActuallyExists = true;

          // if we were expecting the entry, skip it;
          // if we were NOT expecting the entry, allow it to be rewritten
          // so the file is restored to its original condition
          if (entryShouldExist) {
            weWantToWriteTheEntry = false;
          }
        }
        if (weWantToWriteTheEntry) {
          outStream.putNextEntry(entry);
          int byteCount;
          while ((byteCount = inStream.read(buffer)) != -1) {
            outStream.write(buffer, 0, byteCount);
          }
          outStream.closeEntry();
        }
        entry = inStream.getNextEntry();
      }

      // close and delete the temporary file
      inStream.close();
      inFile.delete();
      // check for invalid state
      if (entryShouldExist != entryActuallyExists) {
        outStream.close(); // close it since we will not be returning it
        // need more helpful exceptions here
        if (entryActuallyExists) {
          throw XMLDataStoreException.fileAlreadyExists(new File(this.getZipEntryName()));
        } else {
          throw XMLDataStoreException.fileNotFound(new File(this.getZipEntryName()), null);
        }
      }
      outStream.putNextEntry(new ZipEntry(this.getZipEntryName()));
      return new OutputStreamWriter(outStream);
    } catch (IOException ex) {
      throw XMLDataStoreException.ioException(ex);
    }
  }
Ejemplo n.º 6
0
  private void storeFile(File file) throws FileNotFoundException, IOException {
    Console.printStatus("Storing: " + file);
    try (FileInputStream inputStream = new FileInputStream(file)) {
      _zipStream.putNextEntry(new ZipEntry(file.getCanonicalPath()));

      // Copy the content to the zip stream:

      int length;
      while ((length = inputStream.read(buffer)) > 0) _zipStream.write(buffer, 0, length);

      _zipStream.closeEntry();
    }
  }
Ejemplo n.º 7
0
  /**
   * Zips byte array.
   *
   * @param input Input bytes.
   * @param initBufSize Initial buffer size.
   * @return Zipped byte array.
   * @throws IOException If failed.
   */
  public static byte[] zipBytes(byte[] input, int initBufSize) throws IOException {
    ByteArrayOutputStream bos = new ByteArrayOutputStream(initBufSize);

    try (ZipOutputStream zos = new ZipOutputStream(bos)) {
      ZipEntry entry = new ZipEntry("");

      try {
        entry.setSize(input.length);

        zos.putNextEntry(entry);

        zos.write(input);
      } finally {
        zos.closeEntry();
      }
    }

    return bos.toByteArray();
  }
Ejemplo n.º 8
0
  public byte[] ZipIt(ArrayList<ResultType> items, String[] algorithm, String[] filetype)
      throws IOException {

    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    ZipOutputStream zos = new ZipOutputStream(baos);
    BufferedInputStream bis;

    String[] files = new String[10000];

    File file;

    int bytesRead;

    byte[] buffer = new byte[1024];

    int j = 0;
    boolean flag;

    for (int i = 0; i < items.size(); i++) {

      ResultType g = (ResultType) items.get(i);

      String folder = g.get("genomeNames");

      file = new File("/storage/brcdownloads/patric2/genomes/" + folder);

      if (!file.exists()) {

        System.err.println("Skipping Folder: " + "/storage/brcdownloads/patric2/genomes/" + folder);
        continue;

      } else {
        for (int k = 0; k < algorithm.length; k++) {
          for (int m = 0; m < filetype.length; m++) {

            flag = false;

            file =
                new File(
                    "/storage/brcdownloads/patric2/genomes/"
                        + folder
                        + "/"
                        + folder
                        + (filetype[m].equals(".fna") ? "" : algorithm[k])
                        + filetype[m]);

            if (!file.exists()) {
              System.err.println("Skipping File: " + file.getAbsolutePath());
            } else {
              // System.out.println("File: " + file.getAbsolutePath());
              for (int z = 0; z < files.length; z++) {
                if (files[z] != null && files[z].equals(file.getAbsolutePath())) {
                  flag = true;
                  break;
                }
              }
              // System.out.println(flag);
              if (!flag) {
                files[j] = file.getAbsolutePath();
                j++;
              }
            }
          }
        }
      }
    }
    // System.out.println(j);
    if (j > 0) {
      for (int i = 0; i < j; i++) {
        file = new File(files[i]);
        bis = new BufferedInputStream(new FileInputStream(file));
        ZipEntry entry = new ZipEntry(file.getName());
        zos.putNextEntry(entry);
        while ((bytesRead = bis.read(buffer)) != -1) {
          zos.write(buffer, 0, bytesRead);
        }
        bis.close();
        zos.closeEntry();
      }
      zos.close();
    }

    return baos.toByteArray();
  }
  public static void main(String[] args) throws IOException {

    int servPort = Integer.parseInt(args[0]);

    String ksName = "keystore.jks";
    char ksPass[] = "password".toCharArray();
    char ctPass[] = "password".toCharArray();
    try {
      KeyStore ks = KeyStore.getInstance("JKS");
      ks.load(new FileInputStream(ksName), ksPass);
      KeyManagerFactory kmf = KeyManagerFactory.getInstance("SunX509");
      kmf.init(ks, ctPass);
      SSLContext sc = SSLContext.getInstance("SSL");
      sc.init(kmf.getKeyManagers(), null, null);
      SSLServerSocketFactory ssf = sc.getServerSocketFactory();
      SSLServerSocket s = (SSLServerSocket) ssf.createServerSocket(servPort);

      while (true) {
        // Listen for a TCP connection request.
        SSLSocket c = (SSLSocket) s.accept();

        BufferedOutputStream out = new BufferedOutputStream(c.getOutputStream(), 1024);
        BufferedInputStream in = new BufferedInputStream(c.getInputStream(), 1024);

        byte[] byteBuffer = new byte[1024];
        int count = 0;
        while ((byteBuffer[count] = (byte) in.read()) != -2) {
          count++;
        }
        String newFile = new String(byteBuffer, 0, count, "US-ASCII");
        FileOutputStream writer = new FileOutputStream(newFile.trim());
        int buffSize = 0;
        while ((buffSize = in.read(byteBuffer, 0, 1024)) != -1) {
          int index = 0;
          if ((index =
                  (new String(byteBuffer, 0, buffSize, "US-ASCII"))
                      .indexOf("------MagicStringCSE283Miami"))
              == -1) {
            writer.write(byteBuffer, 0, buffSize);
          } else {
            writer.write(byteBuffer, 0, index);
            break;
          }
        }
        writer.flush();
        writer.close();

        ZipOutputStream outZip = new ZipOutputStream(new BufferedOutputStream(out));
        FileInputStream fin = new FileInputStream(newFile.trim());
        BufferedInputStream origin = new BufferedInputStream(fin, 1024);
        ZipEntry entry = new ZipEntry(newFile.trim());
        outZip.putNextEntry(entry);

        byteBuffer = new byte[1024];
        int bytes = 0;
        while ((bytes = origin.read(byteBuffer, 0, 1024)) != -1) {
          outZip.write(byteBuffer, 0, bytes);
        }
        origin.close();
        outZip.flush();
        outZip.close();
        out.flush();
        out.close();
      }
    } catch (Exception e) {
      System.err.println(e.toString());
    }
  }