[Webinar] Streamline your web hosting managementRegister Today

x
?
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
Medium Priority
?
794 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 46

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 46

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
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 

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 46

Assisted Solution

by:aikimark
aikimark earned 1336 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 664 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 46

Accepted Solution

by:
aikimark earned 1336 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

Learn to develop an Android App

Want to increase your earning potential in 2018? Pad your resume with app building experience. Learn how with this hands-on course.

Question has a verified solution.

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

Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
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…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses
Course of the Month8 days, 15 hours left to enroll

590 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