Rename a file being inserted into the Zip archive..

Nov 25, 2010 at 5:16 PM

Hi Guys

Is it possible to rename the original file as it is being added to the zip archive..  for example I have a batch of images which have to be zipped up into various zip files, but they need to be renamed based on the zip file it is being added to..  So 1234.jpg being the original file name will be archived into test.zip but saved as 1234South.jpg..

I am presently just copying the images to a folder and zipping up the contents per zip file needed, but the copying takes a very very long time (I have 200000 images being zipped every 4 hours or so), and I am sure I can do this more efficiently..  I hope you can help..  I am using VBScript to zip my files..

 

Kind regards,

 

Derek.

Coordinator
Nov 25, 2010 at 6:40 PM

In a .NET language, you can do something like this:

Using zip As New ZipFile
    zip.AddFile("1234.jpg").FileName = "1234South.jpg"
    zip.Save("newzip.zip")
End Using


The AddFile method returns a ZipEntry; you can then get or set properties on that ZipEntry.  The above is equivalent to

Using zip As New ZipFile
    Dim entry as ZipEntry = zip.AddFile("1234.jpg")
    entry.FileName = "1234South.jpg"
    zip.Save("newzip.zip")
End Using

The VBScript equivalent is like this....

Dim zip
Dim entry
Set zip = CreateObject("Ionic.Zip.ZipFile")
Set entry = zip.AddFile ("1234.jpg")
entry.FileName = "1234South.jpg"
zip.Name = "newzip.zip"
zip.Save
zip.Dispose
Set entry = Nothing
Set zip = Nothing


Nov 26, 2010 at 9:02 AM

Hi Cheeso

 

The VBScript version worked well for me..  Thank you and another cheeky question, in the same scenario but I already have an existing zip file, how can I add the file into it rather than create the zip.. 

I'll leave you alone after that :)

Many thanks

 

Derek.

Nov 26, 2010 at 11:46 AM

PS I am trying this code where newzip.zip exists already.  When I run it I get an error saying it exists..  If I delete the existing newzip.zip then it creates a new one with the file zipped up.

Set Entry = zip.UpdateFile("c:\certreq1.txt")
entry.FileName = "C:\archive1.txt"  
zip.Name = "c:\newzip.zip"
zip.Save

Thanks

 

Derek.

Coordinator
Nov 27, 2010 at 1:05 AM

you need to call zip.Initialize. 

like this:

Dim zip
Set zip = CreateObject("Ionic.Zip.ZipFile")
zip.Initialize filename

At that point you can call UpdateFile. 

See this page in the documentation for more information.

Nov 27, 2010 at 6:34 PM

Hi Cheeso

That's what I missed..  It works a treat for me.. 

Many thanks,

 

Derek.

Coordinator
Nov 30, 2010 at 11:47 PM

Glad to hear it.

Cheers.