Answered How do I use the .NET System.IO.Compression Namespace and it's classes?

Cecil

19+ years progress programming and still learning.
Windows (32 bit) WebSpeed Workshop 11.2
.NET 3.5. Installed.

So I tried doing the following:
Code:
USING System.IO.Compression.* FROM ASSEMBLY.
 
DEFINE VARIABLE obj_Zip AS ZipFile NO-UNDO.

I then get the following error message when doing a syntax check.

OE-Error.png

I have this as client side startup parameter:
-assemblies C:\OpenEdge\WRK112\

In the C:\OpenEdge\WRK112\ directory I have the assemblies.xml file.

In the assemblies.xml file I have this:

Code:
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<references>
<assembly name="System.IO.Compression, Version=4.0.0.0,
Culture=neutral, PublicKeyToken=b77a5c561934e089"/>
<assembly name="System.IO.Compression.FileSystem, Version=4.0.0.0,
Culture=neutral, PublicKeyToken=b77a5c561934e089"/>
</references>

What else do I need to do?
 

Cecil

19+ years progress programming and still learning.
Oh the shame, I'm still using WindowsXP and .NET 4.5 is not available.
 

Cecil

19+ years progress programming and still learning.
Problem resolved changed to another development PC running running Windows 7 with .NET 4.5.

Here is the following code to ZIP up the contents of a directory unfortunately only works on Windows.

Code:
USING System.IO.Compression.* FROM ASSEMBLY.
DEFINE VARIABLE obj_Zip     AS  ZipFile NO-UNDO.
DEFINE VARIABLE zipPath     AS CHARACTER   NO-UNDO.
DEFINE VARIABLE startPath   AS CHARACTER   NO-UNDO.
FILE-INFO:FILE-NAME = '.'.

startPath   = FILE-INFO:FULL-PATHNAME + "/temp".
zipPath     = FILE-INFO:FULL-PATHNAME + "/package.zip".

OS-DELETE VALUE(zipPath).

ZipFile:CreateFromDirectory(startPath, zipPath).
 

RealHeavyDude

Well-Known Member
I am a happy user of tar, gzip & gunzip on *nix and winzip on Windoze from OS-COMMAND ...

Regards, RealHeavyDude.
 

Rob Fitzpatrick

ProgressTalk.com Sponsor
For those who want a non-commercial option on Windows, 7zip is a good alternative to Winzip. It also features a command-line version and a COM interface.
 

Cecil

19+ years progress programming and still learning.
I am a happy user of tar, gzip & gunzip on *nix and winzip on Windoze from OS-COMMAND ...

Regards, RealHeavyDude.

I guess I was more interested in how to interface with .NET from the ABL without all that GUI stuff. I was just trying .NET's ZipFile as my first proof of concept. But in reality it would be reasonable if ABL had a built-in capabilities to handle the industry standard of file compression GZIP+TAR, ZIP and anything else I've missed. For goodness sakes, every other free language seams to have compression classes readily available why can't the ABL.
 

RealHeavyDude

Well-Known Member
There are many places where the ABL lacks functionality compared to the other 2 big ones ( Java and .NET ) ...

That's one reason why it does need a lot of love and passion to be an ABL programmer. But then again, isn't the same true for any other given technology?

Heavy Regards, RealHeavyDude.
 

Cecil

19+ years progress programming and still learning.
There are many places where the ABL lacks functionality compared to the other 2 big ones ( Java and .NET ) ...

That's one reason why it does need a lot of love and passion to be an ABL programmer. But then again, isn't the same true for any other given technology?

Heavy Regards, RealHeavyDude.

I know that PSC has a Review Committy Planel which apprantly goes through one-by-one all the new feature requests and then narrows them down to the shortfall. I have no idea how frequently they do this. I think they then go to their largest customers and asking them if they would require any of the new proposed features and that what goes through to be designed and implement.

When I heard this I asked "Why don't you ask us (the developers) to get involved as well with the discussion process". I was told "The shared holders would not like it." Errr.

<rant>Sometime I hate having to defend myself for being a Progress/OpenEdge developer. </rant>
 

RealHeavyDude

Well-Known Member
I am completely with you. And I too hate to defend myself for working such an "uncool" technology. Where "uncool" should be substituted with unknown ...
 

Cecil

19+ years progress programming and still learning.
Nearly 10 years later, I've finally written a .NET zip routine that will ZIP the contents of a folder.

In this example the Session Temp Directory is copied into a zip file and skipping the files that are in use.

Code:
block-level on error undo, throw.

using System.IO.Compression.CompressionLevel from assembly.
using System.IO.Compression.ZipArchive from assembly.
using System.IO.Compression.ZipArchiveEntry from assembly.
using System.IO.Compression.ZipArchiveMode from assembly.
using System.IO.FileAccess from assembly.
using System.IO.FileMode from assembly.
using System.IO.FileShare from assembly.
using System.IO.FileStream from assembly.
using System.IO.Path from assembly.
using System.IO.Stream from assembly.

define temp-table ttSourceFile
    field fullpathname as character
    field filename     as character.

/* ********************  Preprocessor Definitions  ******************** */

/* ************************  Function Prototypes ********************** */


function IsFileLocked returns logical private
    (input filename as character) forward.


/* ***************************  Main Block  *************************** */

define variable sourceFolderPath as character no-undo.
define variable zipFile          as character no-undo.
define variable filename         as character no-undo.
define variable files            as class     "System.String[]" no-undo.

assign
    sourceFolderPath = session:temp-directory
    zipFile          = sourceFolderPath + substitute('\&1.zip', iso-date(today)) .

files = System.IO.Directory:GetFiles(sourceFolderPath).

define variable indexpos as integer no-undo.

do indexpos = 1 to files:length:

    filename = files:GetValue(indexpos - 1).
  
    //Exclude all zip files.
    if filename matches "*.zip" then
        next.
      
    file-info:file-name = filename.
  
    if file-info:file-create-date lt today then
    do:
      
        if IsFileLocked(input file-info:full-pathname) then
            next.
      
        create ttSourceFile.
      
        assign
            ttSourceFile.filename     = Path:GetFileName(file-info:full-pathname) //
            ttSourceFile.fullpathname = file-info:full-pathname.
      
    end.   
end.

run CreateZIPFile(input zipFile).

file-info:file-name = zipFile.

message file-info:full-pathname
    view-as alert-box info.

return.

finally:
  
    empty temp-table ttSourceFile.
  
end finally.



/* **********************  Internal Procedures  *********************** */

procedure CreateZIPFile private:
    /*------------------------------------------------------------------------------
     Purpose:
     Notes:
    ------------------------------------------------------------------------------*/
    define variable fileEntry        as class ZipArchiveEntry no-undo.
    define variable fileEntryStream  as class Stream          no-undo.
    define variable sourceFileStream as class FileStream      no-undo.
    define variable zipFileStream    as class FileStream      no-undo.
    define variable archive          as class ZipArchive      no-undo.
  
    define input parameter zipFile as character no-undo.
  
    zipFileStream  = new FileStream(zipFile, FileMode:Create).

    archive = new ZipArchive(zipFileStream, ZipArchiveMode:Create).

    for each ttSourceFile:
      
        fileEntry = archive:CreateEntry(ttSourceFile.filename, CompressionLevel:Fastest). 
        fileEntryStream = fileEntry:Open().
  
        sourceFileStream  = System.IO.File:OpenRead(ttSourceFile.fullpathname).
      
        if sourceFileStream:CanRead then
        do:
            sourceFileStream:CopyTo(fileEntryStream).
            sourceFileStream:Close().
          
        end.
      
        //Closes the current stream and releases any resources associated with the current stream.
        fileEntryStream:Close().
      
      
    end.
  
    catch e1 as Progress.Lang.Error :
              
        message e1:GetMessage(1)
            view-as alert-box error.
              
    end catch. 
  
    finally:
      
//Clears buffers for this stream and causes any buffered data to be written to the file.
        zipFileStream:Flush().

// This method finishes writing the archive and releases all resources used by the ZipArchive object.
        archive:Dispose(). //Releases all resources used by the ZipArchive object.
  
    end finally.
  
  
end procedure.


/* ************************  Function Implementations ***************** */

function IsFileLocked returns logical private
    ( input sourceFile as character ):
    /*------------------------------------------------------------------------------
     Purpose: TEst to see if a file is locked by another process
     Notes:
    ------------------------------------------------------------------------------*/ 
    define variable fs as class FileStream no-undo.
      
    fs = System.IO.File:Open(sourceFile, FileMode:Open, FileAccess:Read, FileShare:None).
  
    return false.
  
    catch IOe as System.IO.IOException:
      
        //message IOe:message.
      
        return true.
    end catch.
  
    catch e1 as Progress.Lang.Error :
        return true.
    end catch.
  
    finally:
      
        if valid-object(fs) then
        do:
            fs:Close().
            delete object fs.
        end.   
    end finally.
      
end function.
 
Last edited:
Top