Advertisement
Guest User

zipCurrentProject

a guest
Jan 4th, 2019
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Groovy 4.90 KB | None | 0 0
  1. /* :name=       Zip OmegaT project :description=
  2.  *
  3.  *
  4.  *              THIS SCRIPT WAS SPONSORED BY
  5.  *                 ==   cApStAn sprl   ==
  6.  *                     www.capstan.be
  7.  *
  8.  *
  9.  * #Purpose:    Close the current project and pack it as a zip file named as the project.
  10.  * #Files:      Writes zip file in the current project's root folder containing all the files
  11.  *              included in the current project's root folder.
  12.  * #Steps:      1. Get the current project name/location
  13.  *              2. Close the project
  14.  *              3. Pack the directory found in p.1, into a zip file.
  15.  *              4. Open the project again.
  16.  *
  17.  *
  18.  * @author:     Kos Ivantsov
  19.  * @date:       2019-01-04
  20.  * @version:    0.1.4
  21.  */
  22.  
  23. import java.util.zip.ZipFile
  24. import java.util.zip.ZipEntry
  25. import java.util.zip.ZipOutputStream
  26. import java.util.regex.Pattern
  27. import java.io.File
  28. import java.io.FileInputStream
  29. import java.io.FileOutputStream
  30. import java.io.IOException
  31. import java.nio.file.Files
  32. import java.nio.file.Paths
  33. import java.nio.file.StandardCopyOption
  34. import org.omegat.core.Core
  35. import org.omegat.util.Preferences
  36. import org.omegat.util.StringUtil
  37. import org.omegat.core.CoreEvents
  38.  
  39. import static javax.swing.JOptionPane.*
  40. import static org.omegat.util.Platform.*
  41.  
  42. utils = (StringUtil.getMethods().toString().findAll("format")) ? StringUtil : StaticUtils
  43.  
  44. title = res.getString("name") ? res.getString("name") : "Zip OmegaT project"
  45.  
  46. String.metaClass.alert = { ->
  47.         showMessageDialog null, delegate, title, INFORMATION_MESSAGE
  48.         false
  49. }
  50.  
  51. prop = project.projectProperties
  52. if (!prop) {
  53.     message = res.getString("noProject") ? res.getString("noProject") : "No project loaded."
  54.     console.clear()
  55.     console.println(title + "\n" + "="*title.size() + "\n" + message)
  56.     message.alert()
  57.     return
  58. }
  59.  
  60. def dir = prop.getProjectRoot()
  61. def folder = new File(dir)
  62. def name = folder.getName()
  63. def parent = folder.getAbsoluteFile().getParent()
  64. def zipFileName = parent + File.separator + name + ".zip"
  65.  
  66. public class NonEmpty {
  67.     private void putDummy(File folder) {
  68.         folder.listFiles().each {
  69.             try {
  70.                 if (it.isDirectory()) {
  71.                     def subfiles = it.listFiles()
  72.                     if (it.directorySize() == 0) {
  73.                         putDummy(it)
  74.                         }
  75.                     if ( subfiles.length == 0) {
  76.                         new File (it, ".empty") << ''
  77.                     }
  78.                 }
  79.             } catch (IOException e) {
  80.                 e.printStackTrace()
  81.                 }
  82.         }
  83.     }
  84. }
  85.  
  86. public class ZipFiles {
  87.     List<String> filesListInDir = new ArrayList<String>()
  88.  
  89.     private void zipDirectory(File dir, String zipDirName) {
  90.         try {
  91.             populateFilesList(dir)
  92.             //now zip files one by one
  93.             //create ZipOutputStream to write to the zip file
  94.             FileOutputStream fos = new FileOutputStream(zipDirName)
  95.             ZipOutputStream zos = new ZipOutputStream(fos)
  96.             for(String filePath : filesListInDir){
  97.                 //for ZipEntry we need to keep only relative file path, so we used substring on absolute path
  98.                 ZipEntry ze = new ZipEntry(filePath.substring(dir.getAbsolutePath().length()+1, filePath.length()))
  99.                 zos.putNextEntry(ze)
  100.                 //read the file and write to ZipOutputStream
  101.                 FileInputStream fis = new FileInputStream(filePath)
  102.                 byte[] buffer = new byte[1024]
  103.                 int len
  104.                 while ((len = fis.read(buffer)) > 0) {
  105.                     zos.write(buffer, 0, len)
  106.                 }
  107.                 zos.closeEntry()
  108.                 fis.close()
  109.             }
  110.             zos.close()
  111.             fos.close()
  112.         } catch (IOException e) {
  113.             e.printStackTrace()
  114.         }
  115.     }
  116.  
  117.     private void populateFilesList(File dir) throws IOException {
  118.         File[] files = dir.listFiles()
  119.         for(File file : files){
  120.             if(file.isFile()) filesListInDir.add(file.getAbsolutePath())
  121.             else populateFilesList(file)
  122.         }
  123.     }
  124. }
  125.  
  126. new NonEmpty().putDummy(folder)
  127. org.omegat.gui.main.ProjectUICommands.projectClose()
  128.  
  129. Timer timer = new Timer().schedule({
  130.     console.clear()
  131.     String createZip = res.getString("createZip") ? res.getString("createZip") : "Creating {0}..."
  132.     console.println(utils.format(createZip, zipFileName))
  133.     sleep 5000
  134.     new ZipFiles().zipDirectory(folder, zipFileName)
  135.     String createdZip = res.getString("createdZip") ? res.getString("createdZip") : "{0} created."
  136.     console.println(utils.format(createdZip, zipFileName))
  137.     org.omegat.gui.main.ProjectUICommands.projectOpen(folder)
  138.     console.print(res.getString("loadProject") ? res.getString("loadProject") : " Loading the project again...")
  139.     console.println("\n-----")
  140. } as TimerTask, 1000)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement