Creating a ZIP file throws an error

Creating a ZIP file throws an error

dleesuk
Advocate Advocate
2,168 Views
8 Replies
Message 1 of 9

Creating a ZIP file throws an error

dleesuk
Advocate
Advocate

Hi all (again),

I have, hopefully, an easy problem to solve.  😊

 

I use the following code to create a zip file
(it's very early in the testing phase, so please ignore my naming convention; Var1)

 

 

Dim oFilePath As String
Dim oNewPath As String

'Get path from other external routine. The contents of which will be zipped up
oFilePath = SharedVariable("Var1")

'Create new path/location for zip file
oNewPath = SharedVariable("Var1") + "\ZIPPER\"
'MessageBox.Show("The new path where the zip file will live is " & oNewPath, "iLogic")


If Not System.IO.Directory.Exists(oNewPath) Then
    System.IO.Directory.CreateDirectory(oNewPath)
End If

'------------------------------------
'Add some check for FILE EXISTS, etc.
'------------------------------------

'Create zip file called ZipTest (modify to suit)
System.IO.Compression.ZipFile.CreateFromDirectory(oFilePath, oNewPath & "ZipTest.zip")

SharedVariable.RemoveAll()

 

 

 However, it throws the following error up on completion....

 

  • "The process cannot access the file 'C:\Users\.....my_folders.....\ZipTest.zip' because it is being used by another process."

 

It creates the zip file in the correct place with the correct contents, but I can't seem to get the error to go away.

 

I've looked on various sites (Stack Overflow, HubSpot, etc.) to see if I can get a solution but, as previously mentioned in other posts, my knowledge of iLogic and VB.net is very limited, and I can't get anything.

 

Can anyone help??

 


Regards

Darren
0 Likes
Accepted solutions (1)
2,169 Views
8 Replies
Replies (8)
Message 2 of 9

vpeuvion
Advocate
Advocate
Accepted solution

 

Hello,

If I understood correctly, you are trying to create a zip file from a folder and you want to place it in that same folder.
You are reading it to compress it and at the same time writing to this folder. This is what causes this error.
Try changing the destination path outside of this folder and your code should work.

Vincent.

0 Likes
Message 3 of 9

dleesuk
Advocate
Advocate

Strangely, that's exactly what I thought when I woke up in the middle of the night  🤣

Thank you, @vpeuvion 


Regards

Darren
Message 4 of 9

dleesuk
Advocate
Advocate

So, @vpeuvion.....
I accepted your solution to the above issue because it worked without issue.

And it has worked faultlessly all day.  Thanks again!!

 

However,  after slightly altering my code to send the ZIP file to a different folder again, the problem is back and I now don't know how to get around it.  It will generate the required ZIP file, however, it throws a wobbler (the same error as before) and puts nothing into the it. Thus, an empty ZIP file.

 

I'm stumped

 

Any more ideas???


Regards

Darren
0 Likes
Message 5 of 9

vpeuvion
Advocate
Advocate

Hi,
What do you mean by "slightly altering my code"?
This modification is probably the cause of the reappearance of the error. It creates an empty zip file which suggests that the creation command is correct but that reading the information to be compressed is impossible.
I suggest you go back in creating the code to the moment the error reappeared and check that it is no longer present. Then add the modified part in stages to locate the source of the error.
Perhaps you can share the or part of the code with the modifications you have made. Others or I may be able to help you.

Vincent.

0 Likes
Message 6 of 9

dleesuk
Advocate
Advocate

I altered my code to point it to another location.  WHen it didn't work, I pointed it back to the original location and it still refused to work properly  🙁

 

This it the code I use to ZIP the folder in question.
As mentioned previously,  I'm not a coder, so please forgive me if it's a bit 'nasty'

'path to save ZIP file received from other rule
oZipFolder = RuleArguments("FILE LOC FOR DXF")
'MessageBox.Show("oZipFolder = " & oZipFolder, "iLogic")

oFileName = ThisDoc.FileName(False) 'no extension
'MessageBox.Show("oFileName = " & oFileName, "iLogic")

'Temporary locations for testing purposes
'Dim oZipFolder As String = "C:\Users\Darren Lees\OneDrive\CAD\iLogic\DXF Testing\L Drive\"
'Dim oZipFolder As String = "C:\Users\Darren Lees\OneDrive - Osbit\Desktop\"

If Not System.IO.Directory.Exists(oZipFolder) Then
	System.IO.Directory.CreateDirectory(oZipFolder)
End If

'Check to see if location for ZIP file is correct
'MessageBox.Show("Your ZIP info: " & oZipFolder + oFileName & " Rev " & SharedVariable("HighestRev") & ".zip")

'Generate ZIP file
If System.IO.File.Exists(oZipFolder & oFileName & " Rev " & SharedVariable("HighestRev") & ".zip")
	'***--- used SharedVariable as I don't know how to pass more than one argument using NameValueMap ---***
	'If ZIP file with same title exists, delete it and create another one
	My.Computer.FileSystem.DeleteFile(oZipFolder + oFileName & " Rev " & SharedVariable("HighestRev") & ".zip")
	System.IO.Compression.ZipFile.CreateFromDirectory(oZipFolder, oZipFolder + oFileName & " Rev " & SharedVariable("HighestRev") & ".zip")
Else
	'...otherwise create ZIP file
	System.IO.Compression.ZipFile.CreateFromDirectory(oZipFolder, oZipFolder + oFileName & " Rev " & SharedVariable("HighestRev") & ".zip")
End If

 

I just find it confusing that it worked for a while.  The only change I made was to direct it to a different path further up the tree (oZipFolder), so further away from the folder being zipped, and it stopped working!!

 

This is the code where I get the information to send the correct path to my ZIP rule.

oFileName = ThisDoc.FileName(False) 'no extension
'MessageBox.Show("Actual file name (no ext.) = " & oFileName, "iLogic")

'Strip name from file name for DXF file location
Dim FolderName As String = Mid(oFileName, 1, 7)
'MessageBox.Show("Stripped folder name = " & FolderName, "iLogic")

'paths for concept testing or live testing
'Dim DrvName = "C:\Users\Darren Lees\OneDrive - Osbit\Desktop\DXF Testing\"  'drive used for concept testing
Dim DrvName = "C:\Users\Darren Lees\OneDrive\CAD\iLogic\DXF Testing\L Drive\"  'drive used for concept testing
'Dim DrvName As String = "L:\" 'OSBIT Live Drive

'Folder for DXF folder. This can be changed to suit future requirements
Dim UncDxfFolder As String = "\UNCOMPRESSED DXF\"

'Complete destination for DXF and PDF files
Dim oDXF_Folder As String = DrvName & FolderName & UncDxfFolder & oFileName & "\"
'MessageBox.Show("DXF File destination = " & oDXF_Folder, "iLogic")

'Create NameValueMap for DXF destination to send to SAVE TO DXF and SAVE TO DXF rule
Dim oValueDXFMap As Inventor.NameValueMap = ThisApplication.TransientObjects.CreateNameValueMap()
oValueDXFMap.Add("FILE LOC FOR DXF", oDXF_Folder)

'Destination of ZIP folder containing all of the files within oDXF_Folder
Dim oZipFolder = DrvName & FolderName & "\"
'MessageBox.Show("ZIP folder name = " & oZipFolder, "iLogic")

'Create NameValueMap for ZIP destination to send to SAVE TO ZIP rule
Dim oValueZIPMap As Inventor.NameValueMap = ThisApplication.TransientObjects.CreateNameValueMap()
oValueZIPMap.Add("FILE LOC FOR DXF", oZipFolder)

'Create NameValueMap for ZIP file name to send to SAVE TO ZIP rule
'Dim oValueZIPNameMap As Inventor.NameValueMap = ThisApplication.TransientObjects.CreateNameValueMap()
'oValueZIPNameMap.Add("FILE LOC FOR DXF", oFileName)

'This rule determines the highest revision of the items within the DXF drawing
iLogicVb.RunExternalRule("LOOP THRU SHEETS")

'hand path details to other rules
iLogicVb.RunExternalRule("EMPTY FOLDER", oValueDXFMap) 'Empty the folder of everything ready for the next DXFs / PDFs
iLogicVb.RunExternalRule("SAVE TO DXF", oValueDXFMap) 'for DXF files
iLogicVb.RunExternalRule("SAVE TO PDF", oValueDXFMap) 'for PDF file
iLogicVb.RunExternalRule("SAVE TO ZIP", oValueZIPMap) 'folder whose contents are to be zipped up

'OPERATION COMPLETE
''MessageBox.Show("Operation Complete!", "Complete", 'MessageBoxButtons.OK, 'MessageBoxIcon.Information)

''MessageBox.Show("Full Folder Location = " & oDXF_Folder, "iLogic")
''MessageBox.Show("ZIP File Folder Location = " & oZipFolder, "iLogic")

 As mentioned previously,  I'm not a coder, so please forgive me if it's a bit 'nasty'   🤣

 

It should be relatively easy to work through this.  It also sends paths to other routines without issue to create DXFs and a PDF.

Any help on this is hugely appreciated!!

 


Regards

Darren
0 Likes
Message 7 of 9

vpeuvion
Advocate
Advocate

Hi,

Thanks for the information, for me the source of the problem is still the same, you cannot use oZipFolder as the root of the path in the second argument.

vpeuvion_2-1694003186493.png

Here is an example you can test. It will create the zip file in the parent folder of the folder to be compressed and once it is created, it will be moved to the compressed folder.

 
Dim oZipFolder As String = "C:\Users\Darren Lees\OneDrive\CAD\iLogic\DXF Testing\L Drive\" 'path of folder to compress
Dim oZipFileTemp As String = System.IO.Directory.GetParent(System.IO.Directory.GetParent(oZipFolder).ToString).ToString & "\" 'parent folder path
Dim oFilename As String = "MyFile"
Dim HighestRev As String = "A"

System.IO.Compression.ZipFile.CreateFromDirectory(oZipFolder, oZipFileTemp + oFilename & " Rev " & HighestRev & ".zip") 'creating the zip file in the parent folder temporarily
My.Computer.FileSystem.MoveFile(oZipFileTemp + oFilename & " Rev " & HighestRev & ".zip",oZipFolder + oFilename & " Rev " & HighestRev & ".zip") 'moving zip file to compressed folder

I hope this will help you.

Vincent.

 

0 Likes
Message 8 of 9

WCrihfield
Mentor
Mentor

Hi guys.  I don't know whether either of you have read the source documentation for that method or not, but if not, here is a link to that page.

https://learn.microsoft.com/en-us/dotnet/api/system.io.compression.zipfile.createfromdirectory?view=... 

As you can see at that source link, there are 3 variations of that method.  When using the simplest variation, it will not include the base directory itself, only the contents of that base directory.  And the other variations allow you to optionally specify compression level, include base directory or not, and character encoding for entry names.  It also says that if the specified directory is empty, it will create an empty ZIP file.  If the ZIP file already exists, it will throw an IOException.  And if something in the directory can not be added to the ZIP file, it may also throw an error.  When in doubt about any vb.net functionality, I generally go to the source first.

https://learn.microsoft.com/en-us/dotnet/api/ 

The one thing that was not super clear was how deep it will go (only include what's directly in that folder; or also include all sub directories and all of their contents).

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 9 of 9

dleesuk
Advocate
Advocate

I didn't think I'd changed that part of the rule.  I was only changing folders to point to.  It was a long day!!!

How didn't I spot that!!    I'll give it a try later this evening and report back.   😁

 

Thanks


Regards

Darren
0 Likes