Making ZIP files, the easy way.

bill

Well-known Member
Joined
Mar 7, 2002
Messages
550
Consider this beautiful code.
It create a file of type ZIP folder then copies a file into it:

Code:
Sub NewZip(sPath) 
'Create empty Zip File 
Dim oFSO, arrHex, sBin, i, Zip 
  Set oFSO = CreateObject("Scripting.FileSystemObject") 
  arrHex = Array(80, 75, 5, 6, 0, 0, 0, _ 
                 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0) 
  For i = 0 To UBound(arrHex) 
    sBin = sBin & Chr(arrHex(i)) 
  Next 
  With oFSO.CreateTextFile(sPath, True) 
      .Write sBin 
      .Close 
  End With 
End Sub 

Sub Zip_File() 
Dim oApp As Object 

  NewZip ("c:\temp\TEST.zip") 
  
  Set oApp = CreateObject("Shell.Application") 

  oApp.Namespace("c:\temp\TEST.zip").CopyHere ("c:\temp\TEST.xls") 

  Set oApp = Nothing 
End Sub

I'm trying to replicate it in C# but the last COPYHERE is proving difficult to translate... Any idea what COPYHERE really means?

Copy to directory? Copy to file? Copy to h e double hockey sticks?

Thanks!
 

Excel Facts

Move date out one month or year
Use =EDATE(A2,1) for one month later. Use EDATE(A2,12) for one year later.
I believe because you are using the Shell.Application.Namespace property, you are saying copy the .xls file to the zip file.

The argument to the namespace object ("c:\temp\TEST.zip") is handling the "To Path", and because the .zip extension is registered (by WinZip or whatever) to be treated as a folder, you are in essence dragging the xls file into the .zip "folder", programmatically of course.

Since C# is .NET also, you should be able to use the same sort of mechanism to capture the "Temp.zip" namespace, and "CopyHere", to it.

No?
Mike.
 
Upvote 0
Mike,
I appreciate your response.

This: 'Since C# is .NET also' means? The original code was VBA.

Namespace in .NET, far as I can tell, doesn't have a connection to file types.
In addition, the VBA invokes COM objs and trying to avoid that in C#.

Finally, although not too big an issue is the VBA uses late binding and C# is all about type safety and defining everything upfront - assuming I can find an early bind equivilant once I actually determine the solution.
 
Upvote 0

Forum statistics

Threads
1,214,655
Messages
6,120,760
Members
448,991
Latest member
Hanakoro

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top