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
692 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
Comment Utility
Would a .zip or .7z format file be acceptable?  I'm not really sure of your criteria.
0
 

Author Comment

by:fmichail
Comment Utility
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
Comment Utility
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
 

Author Comment

by:fmichail
Comment Utility
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
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
LVL 45

Assisted Solution

by:aikimark
aikimark earned 334 total points
Comment Utility
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 27

Assisted Solution

by:Ark
Ark earned 166 total points
Comment Utility
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
Comment Utility
what are the characters PK &  Chr(5) & Chr(6) & String(18, Chr(0))
0
 
LVL 45

Accepted Solution

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

Author Closing Comment

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

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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…

744 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now