Advertisement
okardec

Untitled

Oct 1st, 2020
1,629
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Kotlin 2.52 KB | None | 0 0
  1. package core.base
  2.  
  3.  
  4. /**
  5. uma classe para comprimir/descomprimir em gzip
  6. deve conseguir trabalhar com bytearray/file/string como parametro de entrada
  7. - comprimir em bytearray e arquivo
  8. - descomprimir em byterray, arquivo e string
  9.  
  10. mas veja bem, é uma classe caprichada pq é pequena, quero que vc consiga fazer funcionar assim
  11. val minhaString = GZIP(byteArray).decompress.asString(charsetOpcional)
  12.  */
  13.  
  14. import java.io.*
  15. import java.nio.charset.Charset
  16. import java.util.zip.GZIPInputStream
  17. import java.util.zip.GZIPOutputStream
  18.  
  19. class GZIP {
  20.  
  21.     private var data: ByteArray? = null
  22.  
  23.     /**
  24.      * recebe um byteArray
  25.      */
  26.     constructor(content: ByteArray) {
  27.         data = content
  28.     }
  29.     /**
  30.      * recebe uma scrint que será convertida para byteArray
  31.      */
  32.     constructor(string: String) {
  33.         data = string.toByteArray()
  34.     }
  35.  
  36.     /**
  37.      * rebece o arquivo q retornará o conteudo
  38.      */
  39.     constructor(file: File) {
  40.         if (!file.exists()){
  41.             return;
  42.         }
  43.         data = file.readBytes()
  44.  
  45.     }
  46.  
  47.     val compress = Compress()
  48.  
  49.     inner class Compress {
  50.  
  51.         fun asByteArray(): ByteArray? {
  52.             val bos = ByteArrayOutputStream()
  53.             GZIPOutputStream(bos).buffered(DEFAULT_BUFFER_SIZE).use { it.write(data) }
  54.             return bos.toByteArray()
  55.         }
  56.  
  57.         fun asFile(file: File) {
  58.             ////se existir renomeia
  59.             if (file.exists()) {
  60.                 var rand = System.currentTimeMillis()
  61.                 file.renameTo(File(file.path.replace(".gz"  ,".$rand.gz" )))
  62.             }
  63.             asByteArray().let {
  64.                 if (it != null) {
  65.                     file.writeBytes(it)
  66.                 }
  67.             }
  68.         }
  69.     }
  70.  
  71.  
  72.     val decompress = Decompress()
  73.  
  74.     inner class Decompress {
  75.  
  76.         fun asByteArray(): ByteArray? {
  77.             return GZIPInputStream(data?.inputStream()).buffered(DEFAULT_BUFFER_SIZE).use { it.readBytes() }
  78.         }
  79.         fun asString(charset: Charset = Charsets.UTF_8): String? {
  80.             return asByteArray()?.let { String(it, charset) }
  81.         }
  82.         fun asFile(file: File) {
  83.             ////se existir renomeia
  84.             if (file.exists()) {
  85.                 var rand = System.currentTimeMillis()
  86.                 file.renameTo(File(file.path.replace("." + file.extension  ,".$rand." + file.extension )))
  87.             }
  88.  
  89.             val bytes = asByteArray() ?: return
  90.             asByteArray()?.let { file.writeBytes(it) }
  91.  
  92.         }
  93.  
  94.     }
  95.  }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement