David Wikström Posted January 31, 2018 Posted January 31, 2018 The Zip File function included with the ScriptMaster sample file only works for files, not folders. Does anyone happen to have working code that works for zip-compressing a folder (including subfolders)?
ryan360Works Posted January 31, 2018 Posted January 31, 2018 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!
David Wikström Posted February 1, 2018 Author Posted February 1, 2018 Thanks Ryan - looks promising. I'll post back with sample code if/when I get that set up and working.
john renfrew Posted February 1, 2018 Posted February 1, 2018 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 1
David Wikström Posted February 1, 2018 Author Posted February 1, 2018 Thanks John - I was sort of counting on you here ;-) Impeccable, as always - remind me to buy you an appropriate beverage in Montréal!
Recommended Posts
This topic is 2546 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 accountSign in
Already have an account? Sign in here.
Sign In Now