コード例 #1
0
ファイル: StreamUtilTest.java プロジェクト: newisso/jodd
 @Test
 public void testCopy() {
   AsciiInputStream in = new AsciiInputStream("input");
   StringOutputStream out = new StringOutputStream();
   try {
     StreamUtil.copy(in, out);
   } catch (IOException ioex) {
     fail("StreamUtil.copy " + ioex.toString());
   }
   assertEquals("input", out.toString());
   StreamUtil.close(out);
   StreamUtil.close(in);
 }
コード例 #2
0
ファイル: ZipUtil.java プロジェクト: lxmhope/jodd
  /** Adds byte content into the zip as a file. */
  public static void addToZip(ZipOutputStream zos, byte[] content, String path, String comment)
      throws IOException {
    while (path.length() != 0 && path.charAt(0) == '/') {
      path = path.substring(1);
    }

    if (StringUtil.endsWithChar(path, '/')) {
      path = path.substring(0, path.length() - 1);
    }

    ZipEntry zipEntry = new ZipEntry(path);
    zipEntry.setTime(System.currentTimeMillis());

    if (comment != null) {
      zipEntry.setComment(comment);
    }

    zos.putNextEntry(zipEntry);

    InputStream is = new ByteArrayInputStream(content);
    try {
      StreamUtil.copy(is, zos);
    } finally {
      StreamUtil.close(is);
    }

    zos.closeEntry();
  }
コード例 #3
0
ファイル: ZipUtil.java プロジェクト: lxmhope/jodd
  /** Decompress gzip archive. */
  public static File ungzip(File file) throws IOException {
    String outFileName = FileNameUtil.removeExtension(file.getAbsolutePath());
    File out = new File(outFileName);
    out.createNewFile();

    FileOutputStream fos = new FileOutputStream(out);
    GZIPInputStream gzis = new GZIPInputStream(new FileInputStream(file));
    try {
      StreamUtil.copy(gzis, fos);
    } finally {
      StreamUtil.close(fos);
      StreamUtil.close(gzis);
    }

    return out;
  }
コード例 #4
0
ファイル: ZipUtil.java プロジェクト: lxmhope/jodd
  /**
   * Extracts zip file to the target directory. If patterns are provided only matched paths are
   * extracted.
   *
   * @param zipFile zip file
   * @param destDir destination directory
   * @param patterns optional wildcard patterns of files to extract, may be <code>null</code>
   */
  public static void unzip(File zipFile, File destDir, String... patterns) throws IOException {
    ZipFile zip = new ZipFile(zipFile);
    Enumeration zipEntries = zip.entries();

    while (zipEntries.hasMoreElements()) {
      ZipEntry entry = (ZipEntry) zipEntries.nextElement();
      String entryName = entry.getName();

      if (patterns != null && patterns.length > 0) {
        if (Wildcard.matchPathOne(entryName, patterns) == -1) {
          continue;
        }
      }

      File file = (destDir != null) ? new File(destDir, entryName) : new File(entryName);
      if (entry.isDirectory()) {
        if (!file.mkdirs()) {
          if (file.isDirectory() == false) {
            throw new IOException("Failed to create directory: " + file);
          }
        }
      } else {
        File parent = file.getParentFile();
        if (parent != null && !parent.exists()) {
          if (!parent.mkdirs()) {
            if (file.isDirectory() == false) {
              throw new IOException("Failed to create directory: " + parent);
            }
          }
        }

        InputStream in = zip.getInputStream(entry);
        OutputStream out = null;
        try {
          out = new FileOutputStream(file);
          StreamUtil.copy(in, out);
        } finally {
          StreamUtil.close(out);
          StreamUtil.close(in);
        }
      }
    }

    close(zip);
  }
コード例 #5
0
ファイル: ZipUtil.java プロジェクト: lxmhope/jodd
  /** Compresses a file into gzip archive. */
  public static File gzip(File file) throws IOException {
    if (file.isDirectory() == true) {
      throw new IOException("Can't gzip folder");
    }
    FileInputStream fis = new FileInputStream(file);

    String gzipName = file.getAbsolutePath() + GZIP_EXT;

    GZIPOutputStream gzos = new GZIPOutputStream(new FileOutputStream(gzipName));
    try {
      StreamUtil.copy(fis, gzos);
    } finally {
      StreamUtil.close(gzos);
      StreamUtil.close(fis);
    }

    return new File(gzipName);
  }
コード例 #6
0
ファイル: StreamUtilTest.java プロジェクト: newisso/jodd
  @Test
  public void testCompare() {
    try {
      File file = new File(dataRoot, "file/a.txt");
      FileInputStream in1 = new FileInputStream(file);

      String content = "test file\r\n";
      if (file.length() == 10) {
        content = StringUtil.remove(content, '\r');
      }
      AsciiInputStream in2 = new AsciiInputStream(content);
      assertTrue(StreamUtil.compare(in1, in2));
      StreamUtil.close(in2);
      StreamUtil.close(in1);
    } catch (FileNotFoundException e) {
      fail("StreamUtil.testCloneCompare " + e.toString());
    } catch (IOException e) {
      fail("StreamUtil.testCloneCompare " + e.toString());
    }
  }
コード例 #7
0
ファイル: ZipUtil.java プロジェクト: lxmhope/jodd
  /** Compresses a file into zlib archive. */
  public static File zlib(File file) throws IOException {
    if (file.isDirectory() == true) {
      throw new IOException("Can't zlib folder");
    }
    FileInputStream fis = new FileInputStream(file);
    Deflater deflater = new Deflater(Deflater.BEST_COMPRESSION);

    String zlibFileName = file.getAbsolutePath() + ZLIB_EXT;

    DeflaterOutputStream dos =
        new DeflaterOutputStream(new FileOutputStream(zlibFileName), deflater);

    try {
      StreamUtil.copy(fis, dos);
    } finally {
      StreamUtil.close(dos);
      StreamUtil.close(fis);
    }

    return new File(zlibFileName);
  }
コード例 #8
0
ファイル: StreamUtilTest.java プロジェクト: newisso/jodd
  @Test
  public void testGetBytes() {
    try {
      FileInputStream in = new FileInputStream(new File(dataRoot, "file/a.txt"));
      byte[] data = StreamUtil.readBytes(in);
      StreamUtil.close(in);

      String s = new String(data);
      s = StringUtil.remove(s, '\r');
      assertEquals("test file\n", s);

      in = new FileInputStream(new File(dataRoot, "file/a.txt"));
      String str = new String(StreamUtil.readChars(in));
      StreamUtil.close(in);
      str = StringUtil.remove(str, '\r');
      assertEquals("test file\n", str);
    } catch (FileNotFoundException e) {
      fail("StreamUtil.testGetBytes " + e.toString());
    } catch (IOException e) {
      fail("StreamUtil.testGetBytes " + e.toString());
    }
  }
コード例 #9
0
ファイル: ZipUtil.java プロジェクト: lxmhope/jodd
  /**
   * Adds single entry to ZIP output stream.
   *
   * @param zos zip output stream
   * @param file file or folder to add
   * @param path relative path of file entry; if <code>null</code> files name will be used instead
   * @param comment optional comment
   * @param recursive when set to <code>true</code> content of added folders will be added, too
   */
  public static void addToZip(
      ZipOutputStream zos, File file, String path, String comment, boolean recursive)
      throws IOException {
    if (file.exists() == false) {
      throw new FileNotFoundException(file.toString());
    }

    if (path == null) {
      path = file.getName();
    }

    while (path.length() != 0 && path.charAt(0) == '/') {
      path = path.substring(1);
    }

    boolean isDir = file.isDirectory();

    if (isDir) {
      // add folder record
      if (!StringUtil.endsWithChar(path, '/')) {
        path += '/';
      }
    }

    ZipEntry zipEntry = new ZipEntry(path);
    zipEntry.setTime(file.lastModified());

    if (comment != null) {
      zipEntry.setComment(comment);
    }

    if (isDir) {
      zipEntry.setSize(0);
      zipEntry.setCrc(0);
    }

    zos.putNextEntry(zipEntry);

    if (!isDir) {
      InputStream is = new FileInputStream(file);
      try {
        StreamUtil.copy(is, zos);
      } finally {
        StreamUtil.close(is);
      }
    }

    zos.closeEntry();

    // continue adding

    if (recursive && file.isDirectory()) {
      boolean noRelativePath = StringUtil.isEmpty(path);

      final File[] children = file.listFiles();

      if (children != null && children.length != 0) {
        for (File child : children) {
          String childRelativePath = (noRelativePath ? StringPool.EMPTY : path) + child.getName();
          addToZip(zos, child, childRelativePath, comment, recursive);
        }
      }
    }
  }