Skip to content
View in the app

A better way to browse. Learn more.

FMForums.com

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

when i zip a folder the original folder is locked

Featured Replies

Hi
I 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 not
What 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

 

  • 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

Old version of unzip?

Well thanks for using that... now over 5 years old..

Can I suggest adding

zipOut.closeEntry()

after the line:

zipOut << new FileInputStream(file)

  • Author

Thanks John
I understood the principle
zipOut.closeEntry() did not work but entry.close () yes
best regards

  • Author

it works the first time but after not :|

  • Author

Hi

it is the new FileInputStream (file) which was to be closed

so I create a new var zipOutN  and closed it
Now, 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

 

  • 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 by ericire

  • Author

hello
let'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()
}

 

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 by john renfrew

  • Author

ok
thank's John

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.

  • 3 weeks later...

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.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()

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()
}
" )

Bailey

What is the error??

i just revisited this las tweaked and am pretty sure it all works.

It just says "ERROR"

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()

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

 

Screen Shot 2015-10-08 at 10.36.08 AM.png

  • 4 months later...

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 by jsubiros

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

 

 

What parameter expect in "fm_delete?"   ???? To do what ?

Thanks.

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.

 

Thanks a lot John, ....... it runs perfect now !!!!

Create an account or sign in to comment

Important Information

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

Account

Navigation

Search

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.