Windows Scripting
  Home arrow Windows Scripting arrow Page 2 - Compressed Folders in WSH
ASP Free Forums 
.NET  
ASP  
ASP Code  
ASP.NET  
ASP.NET Code  
BrainDump  
C#  
Code Examples  
Database  
Database Code  
IIS  
Microsoft Access  
MS SQL Server  
Silverlight  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
ASP Web Hosting  
ASP.NET Web Hosting 
Windows Web Hosting
 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
WINDOWS SCRIPTING

Compressed Folders in WSH
By: Nilpo
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 1
    2008-12-29

    Table of Contents:
  • Compressed Folders in WSH
  • Creating a Binary file
  • Getting a Byte Array in VBScript
  • Adding and Extracting files

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Compressed Folders in WSH - Creating a Binary file


    (Page 2 of 4 )

    Now that we can see the binary makeup of the zip file, we can write that binary string to a new file on disk to create our own zip file—except that neither VBScript nor WSH provide a method for writing binary data.  And so, we have problem number two.

    Many of the solutions that I’ve seen in the past for this problem have simply taken the binary data and written its decimal equivalent (as ASCII characters) to a text file using the FileSystemObject.

    Const ForWriting = 2

    Set objFSO = CreateObject("Scripting.FileSystemObject")

    Set objFile = objFSO.OpenTextFile("New Folder.zip", ForWriting, True)

    objFile.Write "PK" & Chr(5) & Chr(6) & String(18, Chr(0))

    objFile.Close

    Sure enough, this approach will work.  This zip format is simple enough that it works on most systems.  However, the problem lies a little deeper.  The FileSystemObject’s methods are designed for creating and writing text files, not binary files.  In other words, a script written on one machine may not work on a machine using a different codebase.  The simple truth is that, while this method works in this particular instance, it’s not a workable solution for creating binary files.  To do that we’ll need to look elsewhere.

    The only native scriptable object capable of handling binary data is the ADO Stream object.  Just our luck, it also provides a method of writing its stream data to a file.

    strPath = "C:zip.zip"

     

    Const adTypeBinary = 1

    Const adTypeText = 2

    Const adSaveCreateNotExist = 1

    Const adSaveCreateOverwrite = 2

    Set objStream = CreateObject("ADODB.Stream")

    You can create an ADO Stream object by connecting to the ADODB.Stream ProgID.  To make it usable, you’ll need to open the stream and define its type as either text or binary.  The choice would seem quite obvious, but herein lies another problem.  If you choose a binary stream type, you must provide a byte array to write to the stream.  VBScript does not provide any method of creating a byte array, so it’s time for yet another workaround.

    objStream.Open

    objStream.Type = adTypeText

    For now, let’s go ahead and write our file as a text stream.  The Open method opens the Stream object and the Type property is used to set the Stream type as text.

    objStream.WriteText ChrB(&h50) & ChrB(&h4B) & ChrB(&h5) & ChrB(&h6)

    For i = 1 To 18

       objStream.WriteText ChrB(&h0)

    Next

    The WriteText method is used to write a string of characters to the Stream object.  Rather than trying to convert every character in the original binary string that we found, I’m just adding each character by its hex value.  VBScript’s ChrB function is fairly undocumented; it returns a binary character for a provided value.  I’m using VBScript’s &h notation to indicate that the provided numbers should be interpreted as hexadecimal values.

    objStream.SaveToFile strPath, adSaveCreateNotExist

    objStream.Close

    Now we can use the Stream object’s SaveToFile method to write to stream data to a file.  The first method is the full path and file name of the file to be written.  The second is a constant value that indicates whether the file should be created if it doesn’t exist or if it should be overwritten.  Finally, the stream is closed with the Close method.

    At this point, we’ve written our binary characters to a file.  However, further examination with our trusty hex editor shows that the ADO Stream object has written two extra unwanted bytes at the beginning of the file.  This is no good.  It turns out that we need to write true binary data.  It also turns out that the ADO Stream object does provide a method of returning a byte array from an existing file, and we can use the dummy file that we’ve just created.

    More Windows Scripting Articles
    More By Nilpo


       · Hello,I was looking for this a long time!Great job!
       · Thank you very much.
     

    WINDOWS SCRIPTING ARTICLES

    - More Windows Scripting Workarounds from Nilpo
    - Overloading Methods and More in VBScript
    - Improving MFC for Windows Vista
    - Regular Expressions in VBScript
    - Working with Dates in WMI
    - Completing Calendars with VBScript Date Func...
    - Building Calendars with VBScript Date Functi...
    - Working With Dates and Times in VBScript
    - Designing WCF DataContract Classes Using the...
    - Understanding Dates and Times in VBScript
    - Working With Arrays in VBScript
    - Compressed Folders in WSH
    - Using .NET Interops in VBScript
    - Nilpo`s Scripting Secrets, Vol I
    - Database operations using Silverlight 2.0 WC...





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 1 Hosted by Hostway
    Stay green...Green IT