Solved

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

Posted on 2014-04-07
9
720 Views
Last Modified: 2014-04-09
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.
0
Comment
Question by:fmichail
  • 4
  • 4
9 Comments
 
LVL 45

Expert Comment

by:aikimark
ID: 39984482
Would a .zip or .7z format file be acceptable?  I'm not really sure of your criteria.
0
 

Author Comment

by:fmichail
ID: 39984496
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
 
LVL 45

Expert Comment

by:aikimark
ID: 39984513
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:fmichail
ID: 39984525
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
 
LVL 45

Assisted Solution

by:aikimark
aikimark earned 334 total points
ID: 39984564
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
 
LVL 28

Assisted Solution

by:Ark
Ark earned 166 total points
ID: 39984807
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
 

Author Comment

by:fmichail
ID: 39986264
what are the characters PK &  Chr(5) & Chr(6) & String(18, Chr(0))
0
 
LVL 45

Accepted Solution

by:
aikimark earned 334 total points
ID: 39986379
They are the file header of a zip file (think of the pkzip origins of this compression technology)
0
 

Author Closing Comment

by:fmichail
ID: 39988894
Solution is excellent, I wish if there is a higher grade to give. Thanks you guys.
0

Featured Post

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question