Jump to content
Server Maintenance This Week. ×

when i zip a folder the original folder is locked


This topic is 2997 days old. Please don't post here. Open a new topic instead.

Recommended Posts

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

 

Link to comment
Share on other sites

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

 

Link to comment
Share on other sites

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

 

Link to comment
Share on other sites

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
Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

  • 4 months later...

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

 

 

  • Like 1
Link to comment
Share on other sites

This topic is 2997 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 account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...

Important Information

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