Exemplo n.º 1
0
  protected void extractFile(
      File srcF,
      File dir,
      InputStream compressedInputStream,
      String entryName,
      Date entryDate,
      boolean isDirectory,
      Integer mode)
      throws IOException, ArchiverException {
    File f = FileUtils.resolveFile(dir, entryName);

    try {
      if (!isOverwrite() && f.exists() && f.lastModified() >= entryDate.getTime()) {
        return;
      }

      // create intermediary directories - sometimes zip don't add them
      File dirF = f.getParentFile();
      if (dirF != null) {
        dirF.mkdirs();
      }

      if (isDirectory) {
        f.mkdirs();
      } else {
        byte[] buffer = new byte[1024];
        int length;
        FileOutputStream fos = null;
        try {
          fos = new FileOutputStream(f);

          while ((length = compressedInputStream.read(buffer)) >= 0) {
            fos.write(buffer, 0, length);
          }

          fos.close();
          fos = null;
        } finally {
          if (fos != null) {
            try {
              fos.close();
            } catch (IOException e) {
              // ignore
            }
          }
        }
      }

      f.setLastModified(entryDate.getTime());

      if (mode != null) {
        ArchiveEntryUtils.chmod(f, mode.intValue(), getLogger());
      }
    } catch (FileNotFoundException ex) {
      getLogger().warn("Unable to expand to file " + f.getPath());
    }
  }
  private void writeFile(File dir, String fname, int mode) throws IOException, ArchiverException {
    File file = new File(dir, fname);
    FileWriter writer = null;

    try {
      if (file.getParentFile() != null) {
        file.getParentFile().mkdirs();
      }

      writer = new FileWriter(file);
      writer.write("This is a test file.");
    } finally {
      IOUtil.close(writer);
    }

    ArchiveEntryUtils.chmod(file, mode, logger, false);
  }