Posted By

karelklic on 02/27/08


Tagged

file utf8 zip diacritics


Versions (?)

Compresses all the files in some directory recursively (zip); solves diacritics problems


 / Published in: Groovy
 

Removes diacritics from file name before adding it to the archive. Archive file name retains diacritics.

  1. import java.util.zip.*
  2. import java.text.*
  3.  
  4. def toBase2(String sText){
  5. boolean bChar = true
  6. int iSize = sText.length()
  7. int i = 0
  8. String sAux = ""
  9.  
  10. for (i=0; i < iSize; i++) {
  11. String sLetter = sText.charAt(i)
  12. sLetter = Normalizer.normalize(sLetter, Normalizer.Form.NFD)
  13. try{
  14. byte[] bLetter = (new String(sLetter)).getBytes("UTF-8")
  15. char cLetter = (char) bLetter[0]
  16. sAux += "" + cLetter
  17. }
  18. }
  19. }
  20. return sAux
  21. }
  22.  
  23. new File(args[0]).eachFileRecurse() { f ->
  24. if (f ==~ /.*zip$/ || !f.isFile()) return
  25. name = f.toString()
  26. zipname = name.substring(0, name.lastIndexOf(".")) + ".zip"
  27. nameInZip = toBase2(name.substring(name.lastIndexOf("\\") + 1))
  28. println nameInZip
  29. out.putNextEntry(new ZipEntry(nameInZip))
  30.  
  31. // nejde napsat out.write(f.readbytes()), protoze na 100mb
  32. // souboru to zahlasi out of memory
  33. ins = f.newInputStream()
  34. buffer = new byte[18024]
  35. while ((len = ins.read(buffer)) > 0)
  36. out.write(buffer, 0, len)
  37.  
  38. out.closeEntry()
  39. out.close()
  40. }

Report this snippet  

You need to login to post a comment.