How to compress files (images) using windows compression (NOT WNZIP) from VB6. It is OK to use vb.net

I am working in VB6, and I need to compress image files using windows compression. I am OK with writing a vb.net code, but then I need to know how to call it from VB6. Please provide the code for compression and decmpression. Thanks for the help.
fmichailAsked:
Who is Participating?
 
aikimarkConnect With a Mentor Commented:
They are the file header of a zip file (think of the pkzip origins of this compression technology)
0
 
aikimarkCommented:
Would a .zip or .7z format file be acceptable?  I'm not really sure of your criteria.
0
 
fmichailAuthor Commented:
Hi aikimark

a zip format is perfectly OK, as long as i do not use the winzip to create it. only .net code in an assemblythat I can register and access in VB6. Thanjs
0
The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

 
aikimarkCommented:
You can probably do this from your VB6 application without having to use the .Net framework.

Additional question...Do you need to compress these files in a batch/bulk/block operation or on a file-by-file basis?
0
 
fmichailAuthor Commented:
aikimark

I need to be ableto compress one or more files every run (I beleive thisis what you mean by batch/bulk/block operation. Thanks
0
 
aikimarkConnect With a Mentor Commented:
Your code should look something like this:
Dim oFSO As Object, oTS As Object, oWsh As Object

Set oFSO = CreateObject( "Scripting.FileSystemObject" )
Set oTS = oFSO.CreateTextFile(compresseddirectoryname, True)
oTS.write("PK" & Chr(5) & Chr(6) & String(18,Chr(0)))
oTS.close

Set oWsh = CreateObject("Shell.Application")
oWsh.Namespace(compresseddirectoryname).CopyHere filenametoaddtocompressedfolder

Open in new window

0
 
ArkConnect With a Mentor Commented:
Here is a complete class:
Option Explicit

Private Declare Function GetFileAttributes Lib "kernel32" Alias "GetFileAttributesA" (ByVal lpFileName As String) As Long
Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)

'Zip file or folder.
'sSource is a file or folder name to zip.
'sDest - zip file name
'waitComplete - if true, returns after zip process complete or immediate otherwise
Public Function Zip(ByVal sSource As String, _
                    ByVal sDest As String, _
                    Optional ByVal waitComplete As Boolean = False) As Boolean
   Dim lAttr As Long
   lAttr = GetFileAttributes(sSource)
   If lAttr = -1 Then
      MsgBox ("File not found or unrecognized source file(s) attributes.")
      Exit Function
   End If
   
'Create empty zip file with zip header
   Dim sZipHeader As String
   sZipHeader = Chr(80) & Chr(75) & Chr(5) & Chr(6) & String(18, Chr(0))
   If Dir(sDest) <> "" Then Kill sDest
   Dim nFile As Integer
   nFile = FreeFile
   Open sDest For Binary Access Write As #nFile
      Put #nFile, , sZipHeader
   Close #nFile
   
   Dim oShell, oSource, oDest
   Set oShell = CreateObject("Shell.Application")
   If (lAttr And vbDirectory) = vbDirectory Then
      Set oSource = oShell.NameSpace(CVar(sSource)).items
      If oSource Is Nothing Then GoTo ErrZip
   Else
      oSource = sSource
   End If
   Set oDest = oShell.NameSpace(CVar(sDest))
  'Copy the file(s) to the compressed folder
   oDest.CopyHere oSource
   If waitComplete Then
     'Wait for compressing to begin
      Sleep 1000
     'wait for lock to release
      On Error Resume Next
      Do
'Attempt to open the file, this causes an Err 70,
'Permission Denied when the file is already open
         nFile = FreeFile
         Open sDest For Append As #nFile
         Close #nFile
         If Err = 0 Then Exit Do
         Sleep 300
         Err.Clear
      Loop
   End If
   Zip = True
ErrZip:
   Set oSource = Nothing
   Set oShell = Nothing
End Function

Public Function UnZip(ByVal sSource As String, Optional ByVal sDest As String) As Boolean
   If sSource = "" Then Exit Function
   If Dir(sSource) = "" Then Exit Function
   If sDest = "" Then sDest = App.Path
   If Right(sDest, 1) <> "\" Then sDest = sDest & "\"
   Dim oShell As Object, oFolder As Object
   Set oShell = CreateObject("Shell.Application")
   Set oFolder = oShell.NameSpace(CVar(sDest))
   oFolder.CopyHere oShell.NameSpace(CVar(sSource)).items
   Set oFolder = Nothing
   Set oShell = Nothing
   UnZip = True
End Function

Open in new window

sample usage:
   Dim zipper As New vbZipper
'zip entire folder synchroneously (waiting to complete)
   If zipper.Zip("C:\Program Files", "C:\pfiles.zip", True) Then
      MsgBox "Complete"
   End If
'zip file async (return immediate)
   Call zipper.Zip("C:\windows\notepad.exe", "C:\notepad.zip")

Open in new window

0
 
fmichailAuthor Commented:
what are the characters PK &  Chr(5) & Chr(6) & String(18, Chr(0))
0
 
fmichailAuthor Commented:
Solution is excellent, I wish if there is a higher grade to give. Thanks you guys.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.