January 31, 20187 yr 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)?
January 31, 20187 yr 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!
February 1, 20187 yr Author Thanks Ryan - looks promising. I'll post back with sample code if/when I get that set up and working.
February 1, 20187 yr 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
February 1, 20187 yr Author Thanks John - I was sort of counting on you here ;-) Impeccable, as always - remind me to buy you an appropriate beverage in Montréal!
Create an account or sign in to comment