Пример #1
0
  /**
   * Adds a single file into the ZipOutputStream with specified entry name.
   *
   * @throws IOException
   */
  private void addFileToZip(ZipOutputStream zipOut, UploadedFile file, String name)
      throws IOException {
    if (log.isTraceEnabled()) {
      log.trace(">> addFileToZip(): " + file);
    }

    ZipEntry entry = new ZipEntry(name);
    zipOut.putNextEntry(entry);

    InputStream in = null;
    try {
      in = file.getInputstream();
      FileUtils.copyStream(file.getInputstream(), zipOut);
      zipOut.closeEntry();
    } finally {
      FileUtils.close(in);
    }

    //        try (InputStream in = file.getInputstream()) {
    //            FileUtils.copyStream(file.getInputstream(), zipOut);
    //            zipOut.closeEntry();
    //        }

    if (log.isTraceEnabled()) {
      log.trace("<< addFileToZip()");
    }
  }
Пример #2
0
  private Attachment prepareAttachment(User user, List<UploadedFile> files, String tags) {
    System.out.println("prepare attachment...");
    if (files.isEmpty()) {
      System.out.println("files are empty");
      if (log.isDebugEnabled()) {
        log.debug("prepareAttachment() : List of files is empty! Nothing to compress.");
      }
      return null;
    }
    if (files.size() == 1) {
      if (log.isTraceEnabled()) {
        log.trace(
            "prepareAttachment() : Single file is being uploaded. Delegating to uploadFile()");
      }
      try {
        return prepareAttachment(
            files.get(0).getFileName(),
            files.get(0).getContentType(),
            user,
            files.get(0).getContents(),
            tags);
      } catch (IOException ex) {
        if (log.isTraceEnabled()) {
          log.trace("prepareAttachment() : I/O exception" + ex);
        }
        return null;
      }
    }

    try {
      // create zip file
      log.trace("prepareAttachment(): Creating zip-file");

      File root = new File(DEFAULT_UPLOAD_DIRECTORY, "user" + user.getId());
      root.mkdirs();
      File file = File.createTempFile("upload_", ".zip", root);

      ZipOutputStream zos = null;

      try {
        //                zos = new ZipOutputStream
        zos = new ZipOutputStream(file);
        zos.setEncoding("utf-8");
        zos.setMethod(ZipOutputStream.DEFLATED);
        zos.setLevel(Deflater.BEST_COMPRESSION);

        for (UploadedFile uf : files) {
          addFileToZip(zos, uf, uf.getFileName());
        }
      } finally {
        FileUtils.close(zos);
      }

      //            try (ZipOutputStream zos = new ZipOutputStream(file)) {
      //                zos.setEncoding("utf-8");
      //                zos.setMethod(ZipOutputStream.DEFLATED);
      //                zos.setLevel(Deflater.BEST_COMPRESSION);
      //
      //                for (UploadedFile uf : files) {
      //                    addFileToZip(zos, uf, uf.getFileName());
      //                }
      //            }

      if (log.isDebugEnabled()) {
        log.debug("prepareAttachment(): Files are saved at " + file);
      }

      if (file.length() > MAX_ZIP_SIZE) {
        file.delete();
        throw new IOException("File too large.");
      }

      // Create attachment
      Attachment att = new Attachment();
      att.setName(file.getName());
      att.setMimeType("application/zip");
      att.setSize(file.length());
      att.setMD5(FileUtils.getMD5(file));
      att.setFileName("user" + user.getId() + "/" + file.getName());

      if (log.isTraceEnabled()) {
        log.trace("<< prepareAttachment()");
      }
      return att;
    } catch (IOException ex) {
      log.error("prepareAttachment(): Failed to upload files. ", ex);
      return null;
    }
  }