• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

JohnSnowLabs / spark-nlp / 4947838414

pending completion
4947838414

Pull #13796

github

GitHub
Merge 30bdeef19 into ef7906c5e
Pull Request #13796: Add unzip param to downloadModelDirectly in ResourceDownloader

39 of 39 new or added lines in 2 files covered. (100.0%)

8632 of 13111 relevant lines covered (65.84%)

0.66 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

32.0
/src/main/scala/com/johnsnowlabs/util/FileHelper.scala
1
/*
2
 * Copyright 2017-2022 John Snow Labs
3
 *
4
 * Licensed under the Apache License, Version 2.0 (the "License");
5
 * you may not use this file except in compliance with the License.
6
 * You may obtain a copy of the License at
7
 *
8
 *    http://www.apache.org/licenses/LICENSE-2.0
9
 *
10
 * Unless required by applicable law or agreed to in writing, software
11
 * distributed under the License is distributed on an "AS IS" BASIS,
12
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
 * See the License for the specific language governing permissions and
14
 * limitations under the License.
15
 */
16

17
package com.johnsnowlabs.util
18

19
import org.apache.commons.io.FileUtils
20

21
import java.io.{File, IOException}
22
import java.nio.charset.Charset
23
import java.nio.file.{Files, Paths}
24
import java.security.MessageDigest
25
import java.text.DecimalFormat
26

27
object FileHelper {
28
  def writeLines(file: String, lines: Seq[String], encoding: String = "UTF-8"): Unit = {
29
    val writer = Files.newBufferedWriter(Paths.get(file), Charset.forName(encoding))
×
30
    try {
×
31
      var cnt = 0
×
32
      for (line <- lines) {
×
33
        writer.write(line)
×
34
        if (cnt > 0)
×
35
          writer.write(System.lineSeparator())
×
36
        cnt += 1
×
37
      }
38
    } catch {
39
      case ex: IOException =>
40
        ex.printStackTrace()
×
41
    } finally if (writer != null) writer.close()
×
42
  }
43

44
  def delete(file: String, throwOnError: Boolean = false): Unit = {
45
    val f = new File(file)
1✔
46
    if (f.exists()) {
×
47
      try {
1✔
48
        if (f.isDirectory)
1✔
49
          FileUtils.deleteDirectory(f)
1✔
50
        else
51
          FileUtils.deleteQuietly(f)
1✔
52
      } catch {
53
        case e: Exception =>
54
          if (throwOnError)
55
            throw e
×
56
          else
57
            FileUtils.forceDeleteOnExit(f)
×
58
      }
59
    }
60

61
  }
62

63
  def generateChecksum(path: String): String = {
64
    val arr = Files readAllBytes (Paths get path)
×
65
    val checksum = MessageDigest.getInstance("MD5") digest arr
×
66
    checksum.map("%02X" format _).mkString
×
67
  }
68

69
  def getHumanReadableFileSize(size: Long): String = {
70
    if (size <= 0) return "0"
×
71
    val units = Array[String]("B", "KB", "MB", "GB", "TB", "PB", "EB")
1✔
72
    val digitGroups = (Math.log10(size) / Math.log10(1024)).toInt
1✔
73
    new DecimalFormat("#,##0.#").format(size / Math.pow(1024, digitGroups)) + " " + units(
1✔
74
      digitGroups)
75
  }
76
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc