ericire Posted September 16, 2015 Posted September 16, 2015 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
ericire Posted September 16, 2015 Author Posted September 16, 2015 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
john renfrew Posted September 16, 2015 Posted September 16, 2015 Well thanks for using that... now over 5 years old.. Can I suggest adding zipOut.closeEntry() after the line: zipOut << new FileInputStream(file)
ericire Posted September 17, 2015 Author Posted September 17, 2015 Thanks JohnI understood the principlezipOut.closeEntry() did not work but entry.close () yesbest regards
ericire Posted September 17, 2015 Author Posted September 17, 2015 it works the first time but after not
ericire Posted September 18, 2015 Author Posted September 18, 2015 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
ericire Posted September 18, 2015 Author Posted September 18, 2015 (edited) 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, 2015 by ericire
ericire Posted September 19, 2015 Author Posted September 19, 2015 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() }
john renfrew Posted September 19, 2015 Posted September 19, 2015 (edited) 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, 2015 by john renfrew
john renfrew Posted September 19, 2015 Posted September 19, 2015 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.
Bailey Kessing Posted October 5, 2015 Posted October 5, 2015 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()}" )
john renfrew Posted October 5, 2015 Posted October 5, 2015 Bailey What is the error?? i just revisited this las tweaked and am pretty sure it all works.
john renfrew Posted October 6, 2015 Posted October 6, 2015 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()
Bailey Kessing Posted October 8, 2015 Posted October 8, 2015 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
jsubiros Posted February 17, 2016 Posted February 17, 2016 (edited) 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, 2016 by jsubiros
john renfrew Posted February 18, 2016 Posted February 18, 2016 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 1
jsubiros Posted February 18, 2016 Posted February 18, 2016 What parameter expect in "fm_delete?" ???? To do what ? Thanks.
john renfrew Posted February 19, 2016 Posted February 19, 2016 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. 1
jsubiros Posted February 19, 2016 Posted February 19, 2016 Thanks a lot John, ....... it runs perfect now !!!!
Recommended Posts
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