/**
   * 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();
  }
  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);
    }
  }
  /**
   * 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();
  }
Example #6
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();
  }