Jump to content

Zip folder with ScriptMaster


This topic is 2248 days old. Please don't post here. Open a new topic instead.

Recommended Posts

Hi David,

  I don't have a module written for this but here is a stackoverflow case that has a few viable examples for accomplishing this in Java that could be modified to work as a ScriptMaster module. I think the one using the ZeroTurnaround library might be the easiest one to implement. Hope that helps!

Link to comment
Share on other sites

David

this works... just native Java, no library required..

 

// ZipFilesOrDirectory( fm_fileIn ; fm_dirIn ; fm_fileOut )
// 15_10_02 JR
// v2.0
// adds list of files to fm_fileOut from fm_dirIn
// or zips a whole directory fm_dirIn to fm_fileOut

import java.util.zip.ZipEntry
import java.util.zip.ZipOutputStream
import java.text.Normalizer
import java.text.Normalizer.Form


SEP = System.getProperty('file.separator')
fm_dirIn = fm_dirIn[-1] == SEP?fm_dirIn : fm_dirIn + SEP

if (!fm_fileIn){
	if (fm_fileOut.contains(fm_dirIn)) {
		return 'ERROR - can\'t recurse'
	} // end if
	try {
		topDir = new File(fm_dirIn) 
		zipOut = new ZipOutputStream ( new FileOutputStream(fm_fileOut))
	
		topDirLength = topDir.absolutePath.length() +1
	
		topDir.eachFileRecurse { file ->
			relPath = file.absolutePath.substring(topDirLength).replace('\\', '/')
			if (file.isDirectory() && !relPath.endsWith('/')) {
				relPath += '/'
			} // end if
			if(relPath[0] =='.'){} else {
				entry = new ZipEntry(relPath)
				entry.time = file.lastModified()
				zipOut.putNextEntry(entry)
				if (file.isFile()) {
					zipOut << new FileInputStream(file)
				} // end if
			} //end if
			
			zipOut.closeEntry()
		} // end each
		zipOut.close()
	}
	catch(Exception e) {
		return e.getMessage()
	} //end try
	return true
	
} else {
	files = fm_fileIn.split('\n')
	byte[] buffer = new byte[1024]
	zout = new ZipOutputStream(new FileOutputStream(fm_fileOut))
	files.each{ item ->
		fin = new FileInputStream( adjust(fm_dirIn + "${item}") )
		zout.putNextEntry(new ZipEntry( adjust("${item}") ))
		while ((len = fin.read(buffer)) > 0){
			zout.write(buffer, 0, len)
		} //end while
		zout.closeEntry()
		fin.close()
	} //end each
	try{
		zout.close()
	} catch (e) {
		return e.getMessage()
	} //end try
	return true

} //end if

 

  • Thanks 1
Link to comment
Share on other sites

This topic is 2248 days old. Please don't post here. Open a new topic instead.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.