使用Scala进行Gzip压缩不会导致归档错误

使用Scala进行Gzip压缩不会导致归档错误,scala,encryption,compression,gzip,Scala,Encryption,Compression,Gzip,我正在对文档进行更改,然后需要使用gzip压缩文档,对其进行加密,然后保存它 每个单独的代码块都按预期通过了单元测试,但它们一起失败,当我打开文件时,我收到一个错误,说它不是存档 下面是基本代码,非常感谢您的帮助,谢谢 val zipped = compressFile1(replaced) def compressFile1(fileContents: String): Array[Byte] = { val bos = new ByteArrayOutputStream() val

我正在对文档进行更改,然后需要使用gzip压缩文档,对其进行加密,然后保存它

每个单独的代码块都按预期通过了单元测试,但它们一起失败,当我打开文件时,我收到一个错误,说它不是存档

下面是基本代码,非常感谢您的帮助,谢谢

val zipped = compressFile1(replaced)

def compressFile1(fileContents: String): Array[Byte] = {
  val bos = new ByteArrayOutputStream()
  val gzs = new GZIPOutputStream(bos)
  gzs.write(fileContents.getBytes("UTF-8"))
  gzs.close()
  val compressed = bos.toByteArray
  bos.close()
  compressed
}
然后对文件进行加密

val encrypted = encrypt(zipped.toString)

def encrypt(value: String): String = {
  val cipher: Cipher = Cipher.getInstance("AES/ECB/PKCS5Padding")
  cipher.init(Cipher.ENCRYPT_MODE, keyToSpec(encryptionPassword))
  Base64.encodeBase64String(cipher.doFinal(value.getBytes("UTF-8")))
}
然后保存它

val file = writeStringToFile(new File("testfile1.gz"), encrypted)

再次感谢您

调用
上的toString
数组[Byte]
实际上返回了类似
[B@4d2f7117
(标准的
toString
实现)。它没有达到您期望的效果,这是

val encrypted = encrypt(new String(zipped))
而不是

val encrypted = encrypt(zipped.toString)