September 16, 201510 yr HiI use this groovy script made by john renfrew ( Thank's ) to zip a folder.It works fine, but the original folder is locked by filemaker and i can't delete it.I try IsGui=False or notWhat can I do ? // ZipDir ( fm_dirIn ; fm_fileOut ) // 10_12_23_JR WORKING // v1.0 // adds all of tree from starting directory to zip file // adapted from Solomon Duskis, http://www.jroller.com import java.io.File import java.io.FileInputStream import java.io.FileOutputStream import java.util.zip.ZipEntry import java.util.zip.ZipOutputStream try { topDir = new File(fm_dirIn) zipOut = new ZipOutputStream ( new FileOutputStream(fm_fileOut) ); topDirLength = topDir.absolutePath.length() topDir.eachFileRecurse{ file -> relPath = file.absolutePath.substring(topDirLength).replace('\\', '/') if ( file.isDirectory() && !relPath.endsWith('/')){ relPath += "/" } entry = new ZipEntry(relPath) entry.time = file.lastModified() zipOut.putNextEntry(entry) if( file.isFile() ){ zipOut << new FileInputStream(file) } } zipOut.close() } catch (Exception e) { e.printStackTrace() } return true
September 16, 201510 yr Author actually when I try to unzip the file with windows, it tells me that the file is invalid: it lacks the name of the original folder (the first level of the tree is empty). But I can unzip it with 7zip
September 16, 201510 yr Well thanks for using that... now over 5 years old.. Can I suggest adding zipOut.closeEntry() after the line: zipOut << new FileInputStream(file)
September 17, 201510 yr Author Thanks JohnI understood the principlezipOut.closeEntry() did not work but entry.close () yesbest regards
September 18, 201510 yr Author Hi it is the new FileInputStream (file) which was to be closed so I create a new var zipOutN and closed itNow, as i said, this zip is invalid because the first folder have'nt any name.It's name must be the same as the folder to be zip, fm_dirIn and i search how to do that.Do you know ? // ZipDir ( fm_dirIn ; fm_fileOut ) // 10_12_23_JR WORKING // v1.0 // adds all of tree from starting directory to zip file // adapted from Solomon Duskis, http://www.jroller.com // without diacriticals chars (accents) in path name import java.io.File import java.io.FileInputStream import java.io.FileOutputStream import java.util.zip.ZipEntry import java.util.zip.ZipOutputStream try { topDir = new File(fm_dirIn) zipOut = new ZipOutputStream ( new FileOutputStream(fm_fileOut) ); topDirLength = topDir.absolutePath.length() topDir.eachFileRecurse{ file -> relPath = file.absolutePath.substring(topDirLength).replace('\\', '/') if ( file.isDirectory() && !relPath.endsWith('/')){ relPath += "/" } entry = new ZipEntry(relPath) entry.time = file.lastModified() zipOut.putNextEntry(entry) if( file.isFile() ){ zipOutN = new FileInputStream(file) zipOut << zipOutN zipOutN.close() } } zipOut.close() } catch (Exception e) { return 'ERROR ' + e.getMessage() //e.printStackTrace() } if (!new File(fm_fileOut).exists()){return 'ERROR'} return true
September 18, 201510 yr Author With a lot of perseverance I finally managed and replace to have parent dir topDirLength = topDir.parent.length()+1 I soon put the whole code with possible deletion Edited September 18, 201510 yr by ericire
September 19, 201510 yr Author hellolet's go with // ZipDir ( fm_dirIn ; fm_fileOut ; fm_delete ) // 10_12_23_JR WORKING & 2015_09_19_E PLASSOT // v2.0 // adds all of tree from starting directory to zip file // adapted from Solomon Duskis, http://www.jroller.com // without diacriticals chars (accents) in path name //bugs corrected to include the original folder and close the inputstream //added option to delete the original folder : if you want put yes in fm_delete and if not put no import java.io.File import java.io.FileInputStream import java.io.FileOutputStream import java.util.zip.ZipEntry import java.util.zip.ZipOutputStream try { topDir = new File(fm_dirIn) ZipOutputStream zipOut = new ZipOutputStream ( new FileOutputStream(fm_fileOut) ); topDirLength = topDir.parent.length()+1 new File(fm_dirIn).eachFileRecurse (){ file -> relPath = file.absolutePath.substring(topDirLength).replace('\\', '/') if ( file.isDirectory() && !relPath.endsWith('/')){ relPath += "/" } entry = new ZipEntry(relPath) entry.time = file.lastModified() zipOut.putNextEntry(entry) if( file.isFile() ){ zipOutN = new FileInputStream(file) zipOut << zipOutN zipOutN.close() } } zipOut.close() if (!new File(fm_fileOut).exists()) {return 'ERROR'} else if (fm_delete == 'yes') { topDir.deleteDir() } else if (fm_delete == 'no') { return true } } catch (Exception e) { return 'ERROR ' + e.getMessage() //e.printStackTrace() }
September 19, 201510 yr Thanks for the improvements... Can I suggest if ( !new File(fm_Out).exists()){ return 'ERROR' } else if (fm_delete) { //this just requires any value in this variable... topDir.deleteDir() } else { return true } Edited September 19, 201510 yr by john renfrew
September 19, 201510 yr If you are using later versions of ScriptMaster you can also make the delete parameter optional by adding a question mark after it ZipDir ( fm_dirIn ; fm_fileOut ; fm_delete? ) Then you can call either of ZipDir ( "directoryA" ; "filename" ; "" ) - to not delete or ZipDir ( "directoryA" ; "filename" ) - to not delete This just ensures an error is not thrown when you pass an empty parameter that you are doing a test on later in the code.
October 5, 201510 yr OK, I have tried the following code and I get an error every time. The old code still works. Suggestions? RegisterGroovy( "ZipDir( fm_dirIn ; fm_fileOut ; fm_delete? )" ; "¶// ZipDir ( fm_dirIn ; fm_fileOut ; fm_delete )// 10_12_23_JR WORKING & 2015_09_19_E PLASSOT// v2.0// adds all of tree from starting directory to zip file// adapted from Solomon Duskis, http://www.jroller.com// without diacriticals chars (accents) in path name//bugs corrected to include the original folder and close the inputstream//added option to delete the original folder : if you want put yes in fm_delete and if not put no import java.io.Fileimport java.io.FileInputStreamimport java.io.FileOutputStreamimport java.util.zip.ZipEntryimport java.util.zip.ZipOutputStream try {topDir = new File(fm_dirIn) ZipOutputStream zipOut = new ZipOutputStream ( new FileOutputStream(fm_fileOut) ); topDirLength = topDir.parent.length()+1 new File(fm_dirIn).eachFileRecurse (){ file -> relPath = file.absolutePath.substring(topDirLength).replace('\\', '/') if ( file.isDirectory() && !relPath.endsWith('/')){ relPath += '/' } entry = new ZipEntry(relPath) entry.time = file.lastModified() zipOut.putNextEntry(entry) if( file.isFile() ){ zipOutN = new FileInputStream(file) zipOut << zipOutN zipOutN.close()} if ( !new File(fm_Out).exists()){ return 'ERROR'} else if (fm_delete) { //this just requires any value in this variable... topDir.deleteDir()} else { return true} } catch (Exception e) {return 'ERROR ' + e.getMessage()//e.printStackTrace()}" )
October 5, 201510 yr Bailey What is the error?? i just revisited this las tweaked and am pretty sure it all works.
October 6, 201510 yr then its to do with.. >>new File(fm_Out).exists() which means the zip file has not been created.. The problem is that the code should be new File(fm_fileOut).exists()
October 8, 201510 yr When I use: RegisterGroovy( "ZipDir( fm_dirIn ; fm_fileOut )" ; "// ZipDir ( fm_dirIn ; fm_fileOut )¶// 10_12_23_JR WORKING¶// v1.2¶// adds all of tree from starting directory to zip file¶// adapted from Solomon Duskis, http://www.jroller.com¶import java.io.File¶import java.io.FileInputStream¶import java.io.FileOutputStream¶import java.util.zip.ZipEntry¶import java.util.zip.ZipOutputStream¶try {¶topDir = new File(fm_dirIn)¶zipOut = new ZipOutputStream ( new FileOutputStream(fm_fileOut) )¶topDirLength = topDir.absolutePath.length()¶topDir.eachFileRecurse{ file ->¶ relPath = file.absolutePath.substring(topDirLength).replace('\\\', '/')¶ if ( file.isDirectory() && !relPath.endsWith('/')){¶ relPath += '/'¶ }¶ entry = new ZipEntry(relPath)¶ entry.time = file.lastModified()¶ zipOut.putNextEntry(entry)¶ if( file.isFile() ){¶ zipOutN << new FileInputStream(file)¶ zipOut << zipOutN¶ zipOutN.close()¶ }¶}¶ zipOut.close()¶} catch (Exception e) {¶ return 'ERROR ' + e.getMessage()¶ //e.printStackTrace()¶}¶if (!new File(fm_fileOut).exists()){return 'ERROR'}¶return true" ) I get a file but when I try to unzip it I get the follow error…image attached
February 17, 201610 yr Hello Bailey, When I use your groovy to ZIP i have this error: ERROR No such property: zipOutN for class: Script1 And I get the zip file with 0 bytes. Any idea ? Thanks. Edited February 17, 201610 yr by jsubiros
February 18, 201610 yr here is some updated code, I use a different technique these days but I am sure this should be OK john // ZipDir ( fm_dirIn ; fm_fileOut, fm_delete? ) // 10_12_23_JR WORKING & 2015_09_19_E PLASSOT // v2.1 // adds all of tree from starting directory to zip file // adapted from Solomon Duskis, http://www.jroller.com import java.util.zip.ZipEntry import java.util.zip.ZipOutputStream if (fm_fileOut.contains(fm_dirIn)) { return 'ERROR' } // 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] =='.'){ //skip hidden files } 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) { //e.printStackTrace() return e.getMessage() //return 'ERROR' } return true
February 19, 201610 yr Yes, you can ignore that.... I think I was experimenting with deleing files after zipping..... the code is commneted out at my end so that's why I didnt copy it to here.
Create an account or sign in to comment