/**
   * 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();
  }
  /**
   * 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);
    }
  }
  /**
   * 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);
    }
  }
示例#4
0
  private boolean saveAndVerify(ZipInputStream data) throws IOException {
    try {
      ZipEntry ze;
      while ((ze = data.getNextEntry()) != null) {
        // Filename + reference to file.
        String filename = ze.getName();
        File output = new File(local_path + filename);

        if (filename.endsWith("/")) {
          output.mkdirs();
        } else {
          if (output.exists()) {
            // Delete the file if it already exists.
            if (!output.delete()) {
              return false;
            }
          }
          if (output.createNewFile()) {
            FileOutputStream out = new FileOutputStream(output);
            byte[] buffer = new byte[1024];
            int count;
            while ((count = data.read(buffer)) != -1) {
              out.write(buffer, 0, count);
            }
          } else {
            return false;
          }
        }
      }
    } catch (Exception e) {
      e.printStackTrace();
      return false;
    } finally {
      data.close();
    }
    return true;
  }
示例#5
0
  /**
   * This method handles PUT requests from the client. PUT requests will come from the applet
   * portion of this application and are the way that images and other files can be posted to the
   * server.
   *
   * @param request the HTTP request object
   * @param response the HTTP response object
   * @exception ServletException
   * @exception IOException
   */
  public void doPut(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException {
    HttpSession session = request.getSession();
    /*
     * The Scan applet will zip all the files together to create a
     * faster upload and to use just one server connection.
     */
    ZipInputStream in = new ZipInputStream(request.getInputStream());

    /*
     * This will write all the files to a directory on the server.
     */
    try {
      try {

        File file = new File("scan");
        Document document = new Document();
        PdfWriter.getInstance(document, new FileOutputStream(file));
        document.open();
        String fileSize = null;
        while (true) {
          ZipEntry entry = in.getNextEntry();
          if (entry == null) {
            break;
          }
          File f = File.createTempFile("translate", entry.getName());
          FileOutputStream out = new FileOutputStream(f);
          FileInputStream inStream = null;
          try {
            int read;
            byte[] buf = new byte[2024];

            while ((read = in.read(buf)) > 0) {
              out.write(buf, 0, read);
            }
            out.close();
            inStream = new FileInputStream(f);
            System.out.println(entry.getSize());
            byte[] b = new byte[inStream.available()];
            inStream.read(b);
            System.out.println(b.length);
            com.itextpdf.text.Image image1 = com.itextpdf.text.Image.getInstance(b);
            image1.scalePercent(30);
            image1.setCompressionLevel(9);
            document.add(image1);
          } finally {
          }
        }
        document.close();
        //                fileSize = CommonUtils.getFileSize(file);
        //                DocumentStoreLogDAO documentStoreLogDAO = new DocumentStoreLogDAO();
        //                DocumentStoreLog documentStoreLog = null;
        //                DocumentStoreLog documentStore =
        // (DocumentStoreLog)session.getAttribute(CommonConstants.DOCUMENT_STORE_LOG);
        //                if(null != documentStore) {
        //                    documentStore.setFileSize(fileSize);
        //                }
        //                Transaction tx = documentStoreLogDAO.getSession().getTransaction();
        //                tx.begin();
        //                documentStoreLogDAO.save(documentStore);
        //                tx.commit();
      } catch (ZipException ze) {
        /*
         * We want to catch each sip exception separately because
         * there is a possibility that we can read more files from
         * the archive even if one of them is corrupted.
         */
        ze.printStackTrace();
      }
    } catch (Exception e) {
      e.printStackTrace();
    } finally {
      in.close();
    }

    /*
     * Now that we have finished uploading the files
     * we will send a reponse to the server indicating
     * our success.
     */

    response.setContentType(CONTENT_TYPE);
    PrintWriter out = response.getWriter();
    out.println("<html><head><title>ImageSrv</title></head></html>");
    out.flush();
    out.close();
    response.setStatus(HttpServletResponse.SC_OK);
  }
  /** initializes internal hash tables with Jar file resources. */
  private byte[] read(String name) {
    try {
      // extracts just sizes only.
      ZipFile zf = new ZipFile(jarFileName);
      Enumeration e = zf.entries();
      while (e.hasMoreElements()) {
        ZipEntry ze = (ZipEntry) e.nextElement();
        if (debugOn) {
          System.out.println(dumpZipEntry(ze));
        }
        htSizes.put(ze.getName(), new Integer((int) ze.getSize()));
      }
      zf.close();

      // extract resources and put them into the hashtable.
      FileInputStream fis = new FileInputStream(jarFileName);
      BufferedInputStream bis = new BufferedInputStream(fis);
      ZipInputStream zis = new ZipInputStream(bis);
      ZipEntry ze = null;
      while ((ze = zis.getNextEntry()) != null) {
        if (ze.isDirectory()) {
          continue;
        }
        if (debugOn) {
          System.out.println("ze.getName()=" + ze.getName() + "," + "getSize()=" + ze.getSize());
        }
        int size = (int) ze.getSize();
        // -1 means unknown size.
        if (size == -1) {
          size = ((Integer) htSizes.get(ze.getName())).intValue();
        }
        byte[] b = new byte[(int) size];
        int rb = 0;
        int chunk = 0;
        while (((int) size - rb) > 0) {
          chunk = zis.read(b, rb, (int) size - rb);
          if (chunk == -1) {
            break;
          }
          rb += chunk;
        }

        if (debugOn) {
          System.out.println(
              ze.getName() + "  rb=" + rb + ",size=" + size + ",csize=" + ze.getCompressedSize());
        }

        if (ze.getName().equals(name)) {
          return b;
        }
      }
    } catch (NullPointerException e) {
      System.out.println("done.");
    } catch (FileNotFoundException e) {
      e.printStackTrace();
    } catch (IOException e) {
      e.printStackTrace();
    }

    return null;
  }
示例#7
0
// =============================================================================