0

私はまたあなたの助けが必要なようです :/

次のコードが与えられた場合:

/**
   * downloads a url (file) to a desired file name
   */
  def downloadFile(url: URL, filename: String) {
    commonOp(url2InputStream(url), filename)
  }

  /**
   * common method for writing data from an inputstream to an outputstream
   */
  def commonOp(is: InputStream, filename: String) {
    val out: OutputStream = file2OutputStream(filename)
    try {
      deleteFileIfExists(filename)
      copy(is, out)
    } catch {
      case e: Exception => println(e.printStackTrace())
    }

    out.close()
    is.close()

  }

  /**
   * copies an inputstream to an  outputstream
   */
  def copy(in: InputStream, out: OutputStream) {
    val buffer: Array[Byte] = new Array[Byte](1024)
    var sum: Int = 0
    Iterator.continually(in.read(buffer)).takeWhile(_ != -1).foreach({ n => out.write(buffer, 0, n); (sum += buffer.length); println(sum + " written to output "); })
  }

  /**
   * handling of bzip archive files
   */
  def unzipFile(fn: String, outputFileName: String) {
    val in = new BZip2CompressorInputStream(new FileInputStream(fn))
    commonOp(in, outputFileName)

  }

これは、本質的には、URL からリモート ファイルをダウンロードして解凍することです。このコードをよりエレガントで副作用のない方法で書く適切な方法はありますか?

ありがとうございました。

4

0 に答える 0