Zip/Compress files in C/SIDE

WaldoWaldo Member Posts: 3,412
edited 2013-04-15 in Navision Attain
I would have to compress all files within a specific folder. Does anyone know how I can do this from within Navision C/SIDE. May be there is some kind of OCX or Automation Server I can use. I thought of using pkzip in a SHELL-command, but isn't there a better solution?

Eric Wauters
MVP - Microsoft Dynamics NAV
My blog

Comments

  • Luc_VanDyckLuc_VanDyck Member, Moderator, Administrator Posts: 3,633
    Have a look at zlib http://www.gzip.org/zlib/
    A DLL of this compression library can be found at http://www.winimage.com/zLibDll/

    I did not try this, but maybe you can call this DLL from C/AL code.
    No support using PM or e-mail - Please use this forum. BC TechDays 2024: 13 & 14 June 2024, Antwerp (Belgium)
  • WaldoWaldo Member Posts: 3,412
    Probably this is a stupid question, but I do not seem to succeed in registring the dll with regsvr32. I get the message "zlib.dll was loaded, but the DllRegisterServer entry point was not found."

    Can you help me with this?

    Have a look at zlib http://www.gzip.org/zlib/
    A DLL of this compression library can be found at http://www.winimage.com/zLibDll/

    I did not try this, but maybe you can call this DLL from C/AL code.

    Eric Wauters
    MVP - Microsoft Dynamics NAV
    My blog
  • Luc_VanDyckLuc_VanDyck Member, Moderator, Administrator Posts: 3,633
    I searched a while and found this page http://www.dogma.net/markn/FAQ.html#Q27 where you can download zlibocx2.dll. This file can be registered using regsvr32.

    Some C/AL code:
    x.InputFileName := 'd:\words_dutch.txt';
    x.OutputFileName := 'd:\words_dutch.zip';
    x.Compress;
    
    x.InputFileName := 'd:\words_dutch.zip';
    x.OutputFileName := 'd:\words_dutch2.txt';
    x.Decompress;
    

    where x is a OCX-variabele, pointing to "zlibIF Class".
    No support using PM or e-mail - Please use this forum. BC TechDays 2024: 13 & 14 June 2024, Antwerp (Belgium)
  • WaldoWaldo Member Posts: 3,412
    Thanks for your time!

    I was looking too for some OCX-possibilities, and one I found:http://www.freedownloadscenter.com/Programming/Visual_Basic_Components_H-P/Maquisistem_Zip_Compress_OCX.html. Now, this always shows a Pop-up, and it didn't seem to work properly (I always got an error 'bad file name' or something).

    Now, I'm trying your possibility, but here, it seems that I only can compress 1 file to 1 zip-file. Now, my main intention was compressing a folder to one file (because, later on, I have to e-mail this file).

    Eric Wauters
    MVP - Microsoft Dynamics NAV
    My blog
  • WaldoWaldo Member Posts: 3,412
    A solution COULD be the following:
    vZipFile := vDirectory + 'NameZipfile.zip';
    
    vZipCommand := 'pkzip ' + vZipFile + ' ' + vDirectory + '*.html';
    
    SHELL(vZipCommand);
    

    Now, this has as major disadvantage that it is a DOS-routine, which means, that it can't use the long Windows-names. E.g. "Waldos zip file.html" becomes something like "waldos~1.HTM". Also the zip-file itself can't have a long file-name. So, if possible, I would prefer another solution.

    Eric Wauters
    MVP - Microsoft Dynamics NAV
    My blog
  • Luc_VanDyckLuc_VanDyck Member, Moderator, Administrator Posts: 3,633
    What version of pkzip are you using? Version 4.00 DOES support long filenames.
    No support using PM or e-mail - Please use this forum. BC TechDays 2024: 13 & 14 June 2024, Antwerp (Belgium)
  • WaldoWaldo Member Posts: 3,412
    Indeed, I was using 2.50.
    I found a better pkzip now at http://burks.brighton.ac.uk/burks/software/compress/install.sw?3.cutezip2.exe. It's not pkzip, but pkzipc and works slightly different. Here is my code:
    vZipFile := vDirectory + 'ZipFile.zip';
    vZipCommand := 'pkzipc -add ' + vZipFile + ' ' + vDirectory + '*.html';
    SHELL(vZipCommand);
    

    Thanks for the help!

    Eric Wauters
    MVP - Microsoft Dynamics NAV
    My blog
  • PWoltersPWolters Member Posts: 8
    Hi! I'm using the Zip Compress OCX from Binarywork in version 3.0.98. Works fine.
  • WaldoWaldo Member Posts: 3,412
    These posts are from 3 years ago ... are there new (free) possibilities to ZIP files from Navision?

    Eric Wauters
    MVP - Microsoft Dynamics NAV
    My blog
  • alberto.donealberto.done Member Posts: 2
    You can use "Microsoft Shell Controls And Automation" to zip an entire folder. Parse FolderItems for specified files.
    //Variable
    IntegerValue	Integer		
    i	Integer		
    Shell32	Automation	'Microsoft Shell Controls And Automation'.Shell	
    SrcFolder	Automation	'Microsoft Shell Controls And Automation'.Folder	
    DstFolder	Automation	'Microsoft Shell Controls And Automation'.Folder	
    SrcItems	Automation	'Microsoft Shell Controls And Automation'.FolderItems3	
    TextFile	File		
    ZipFileName	Text		1024
    SourceFolderName	Text		1024
    
    
    
    //Code
    
    //Zip file Initialization
    TextFile.CREATE(ZipFileName);
    TextFile.TEXTMODE(FALSE);
    
    IntegerValue:=101010256;
    TextFile.WRITE(IntegerValue);
    IntegerValue:=0;
    FOR i:=1 TO 9 DO
      TextFile.WRITE(IntegerValue);
    
    TextFile.CLOSE;
    
    IF CREATE(Shell32) THEN;
    SrcFolder:=Shell32.NameSpace(SourceFolderName);
    DstFolder:=Shell32.NameSpace(ZipFileName);
    SrcItems:=SrcFolder.Items;
    DstFolder.CopyHere(SrcItems);
    

    You can find 'Microsoft Shell Controls And Automation' in all Microsoft Windows computer from W95. \:D/
  • IBIB Member Posts: 20
    Previous way of compressing file is pretty good and simple. But You must take care that compressed folders are supported only in XP/2003 Windows versions. For 98/ME/2000 You need to register additional DLL's which could be taken from MS udpate site (searching here)
    After this, copy files from downloaded file: zipfldr.dll,dunzip32.dll and dzip32.dll into Windows\System or Windows\System32 (depends on OS) directory and register zipfldr.dll with regserv:
    regserv32 X:\windows\system32\zipfldr.dll
    Should work:)
    Thanks for the way described.
    Maybe somebody tried using CAB?:)
  • AndersNordmannAndersNordmann Member Posts: 4
    Hi

    When testing Albertos code example above I'm getting an error: "An exception occurred from an external component."

    What am I doing wrong?

    Specifically the error occours when executing the "DstFolder:=Shell32.NameSpace(ZipFileName);" statement - I'm guessing because ZipFileName points to a file and not a folder?
    // Vars
    IntegerValue	Integer		
    i	Integer		
    Shell32	Automation	'Microsoft Shell Controls And Automation'.Shell	
    SrcFolder	Automation	'Microsoft Shell Controls And Automation'.Folder	
    DstFolder	Automation	'Microsoft Shell Controls And Automation'.Folder	
    SrcItems	Automation	'Microsoft Shell Controls And Automation'.FolderItems3	
    TextFile	File		
    ZipFileName	Text		1024
    SourceFolderName	Text		1024
    
    //Code
    ZipFileName := 'c:\test.zip';
    SourceFolderName := 'c:\test';
    
    TextFile.CREATE(ZipFileName); 
    TextFile.TEXTMODE(FALSE); 
    
    
    IntegerValue:=101010256; 
    TextFile.WRITE(IntegerValue); 
    IntegerValue:=0; 
    FOR i:=1 TO 9 DO 
      TextFile.WRITE(IntegerValue); 
    
    TextFile.CLOSE; 
    
    if isclear(Shell32) then
      CREATE(Shell32);
    
    SrcFolder:=Shell32.NameSpace(SourceFolderName); 
    DstFolder:=Shell32.NameSpace(ZipFileName); 
    SrcItems:=SrcFolder.Items; 
    DstFolder.CopyHere(SrcItems); 
    

    Besides this I'd like to know which modifications I have to make in the code in order to be able to compress (and de-compress) single files?

    Thanks in advance!
  • IBIB Member Posts: 20
    Which version of OS are u using? Is ZIP Folders functionality enabled?
    About working with seperate files, it's the same as working with standart folders. F.e. you can define Folder.CopyHere(filename) and so on.
  • AndersNordmannAndersNordmann Member Posts: 4
    Thanks for your reply.

    I'm on WinXP.
    I don't know if ZIP folders functionality is enabled or disabled - is this configurable? I was hoping to write code that would be executable on any WinXP machine.

    If I understand you correct,
    //Var
    Shell32   Automation   'Microsoft Shell Controls And Automation'.Shell   
    SrcFile   Automation   'Microsoft Shell Controls And Automation'.Folder
    
    //Code
    SomeFileName := 'c:\test.txt';
    SrcFile := Shell32.NameSpace(SomeFileName)
    
    is legal?
  • IBIB Member Posts: 20
    Thanks for your reply.

    I'm on WinXP.
    I don't know if ZIP folders functionality is enabled or disabled - is this configurable? I was hoping to write code that would be executable on any WinXP machine.
    on WindowsXP it's enabled by default.
    You can re-enable it by running (if it is turned off):
    regsvr32 zipfldr.dll
    If I understand you correct,
    //Var
    Shell32   Automation   'Microsoft Shell Controls And Automation'.Shell   
    SrcFile   Automation   'Microsoft Shell Controls And Automation'.Folder
    
    //Code
    SomeFileName := 'c:\test.txt';
    SrcFile := Shell32.NameSpace(SomeFileName);
    
    is legal?
    Not exactly, use like this:
    //Var
    Shell32   Automation   'Microsoft Shell Controls And Automation'.Shell   
    ZIPFolder   Automation   'Microsoft Shell Controls And Automation'.Folder
    ZipFile  Text250
    SomeFileName Text250
    IntVal Integer
    
    
    //Code
    //>>CREATING ZIP FILE
    ZipFile.CREATE('c:\myzipfolder.zip'); //here goes Your zip filename
    ZipFile.TEXTMODE(FALSE); //binary mode
    IntVal:=101010256; 
    //>>writing zip header
    ZipFile.WRITE(IntVal); 
    IntVal:=0;
    FOR i:=1 TO 9 DO 
      ZipFile.WRITE(IntVal); 
    //<<writing zip header
    ZipFile.CLOSE;
    //<<zip file created c:\myzipfolder.zip
    
    CREATE(Shell32); //creating shell automation
    ZIPFolder:=Shell32.NameSpace('c:\myzipfolder.zip'); //ZIPFolder is now our folder in zip format which will be used
    SomeFileName := 'c:\test.txt';
    //>>Copying file to our zip folder
    ZIPFolder.CopyHere(SomeFileName);
    CLEAR(Shell32);
    //<<finish:)
    
    Hope it helps
  • AndersNordmannAndersNordmann Member Posts: 4
    I've tried registering the dll (zipfldr.dll) but the code still errors out.
    // Vars
    IntegerValue	Integer
    i	Integer		
    Shell32	Automation	'Microsoft Shell Controls And Automation'.Shell	
    ZipFolder	Automation	'Microsoft Shell Controls And Automation'.Folder	
    ZipFileName	Text		1024
    ZipFile	File		
    SourceFileName	Text		1024
    
    //Code 
    ZipFile.CREATE('c:\myzipfolder.zip'); //here goes Your zip filename 
    ZipFile.TEXTMODE(FALSE); //binary mode 
    IntegerValue:=101010256;
    ZipFile.WRITE(IntegerValue);
    IntegerValue:=0;
    FOR i:=1 TO 9 DO 
      ZipFile.WRITE(IntegerValue);
    ZipFile.CLOSE; 
    
    CREATE(Shell32);
    ZipFolder:=Shell32.NameSpace('c:\myzipfolder.zip');
    SourceFileName := 'c:\myfile.txt';
    ZipFolder.CopyHere(SourceFileName);
    CLEAR(Shell32); 
    

    The error occours when executing:
    ZipFolder:=Shell32.NameSpace('c:\myzipfolder.zip');
    

    If I replace 'c:\myzipfolder.zip' with 'c:\myzipfolder' (and create the folder beforehand) I get no error, but obviously no zipfile either :(

    Maybe the zipfolder-functionality isn't completely enabled since it looks like windows won't recognize the zipfile as a folder??? - or maybe there is another way to make windows recognize the zipfile as a folder??
  • IBIB Member Posts: 20
    Seems that You've installed archiver which disabled zipfolders functionality and also associated zip files?
    Can You view files in zip folder using explorer? Put one zip file to disk c, open explorer and on left treebar you must see it (folder with zigzag).
  • AndersNordmannAndersNordmann Member Posts: 4
    Thanks for your help IB.

    It seems that registrering zipfldr.dll requires re-boot in order to take effect :o

    It works now, but I think I have to solve this using a different approach :( - registrering the dll from navision is ok, but booting the machine, I think not...

    I think I'll be going with ZlibOCX2.dll which I can export and register when I need it.

    Btw. do you know how well the compression within BLOB-fields work?
  • IBIB Member Posts: 20
    About ZLibOCX - I prefer MS OS features more, but it's your choice :)
    In my case rebooting pc is not a problem :) Also I don't like licensing problems with such OCXes, no support and so on :)
    About BLOB's - actually have no idea :) Till now :)
  • Timo_LässerTimo_Lässer Member Posts: 481
    Is it possible that you provide a similar function for "UnZipFolder"?
    Thanks in advance!
    You can use "Microsoft Shell Controls And Automation" to zip an entire folder. Parse FolderItems for specified files.
    //Variable
    IntegerValue	Integer		
    i	Integer		
    Shell32	Automation	'Microsoft Shell Controls And Automation'.Shell	
    SrcFolder	Automation	'Microsoft Shell Controls And Automation'.Folder	
    DstFolder	Automation	'Microsoft Shell Controls And Automation'.Folder	
    SrcItems	Automation	'Microsoft Shell Controls And Automation'.FolderItems3	
    TextFile	File		
    ZipFileName	Text		1024
    SourceFolderName	Text		1024
    
    
    
    //Code
    
    //Zip file Initialization
    TextFile.CREATE(ZipFileName);
    TextFile.TEXTMODE(FALSE);
    
    IntegerValue:=101010256;
    TextFile.WRITE(IntegerValue);
    IntegerValue:=0;
    FOR i:=1 TO 9 DO
      TextFile.WRITE(IntegerValue);
    
    TextFile.CLOSE;
    
    IF CREATE(Shell32) THEN;
    SrcFolder:=Shell32.NameSpace(SourceFolderName);
    DstFolder:=Shell32.NameSpace(ZipFileName);
    SrcItems:=SrcFolder.Items;
    DstFolder.CopyHere(SrcItems);
    

    You can find 'Microsoft Shell Controls And Automation' in all Microsoft Windows computer from W95. \:D/
    Timo Lässer
    Microsoft Dynamics NAV Developer since 1997
    MSDynamics.de - German Microsoft Dynamics Community - member of [clip]
  • EugeneEugene Member Posts: 309
    ZIP Folders is great of cause but how does one create a NEW zip file with it ? As far as i can find it only allows to decompress from existing zip or add new files to EXISTING zip
  • HelGonHelGon Member Posts: 10
    Hi, I need to create a Zip file password protected. anyone knows
  • CBTCBT Member Posts: 3
    Hi Guys,

    have anyone a solution to make a GZIP (Not ZIP) File ?
    The GZIP dll and the GZIP OCX from Page 1 doesn work in my case.

    Any Ideas ?

    Thx & Greetz
    CBT
  • MatStephensSandAMatStephensSandA Member Posts: 74
    Hi All.

    I (like many before) have to zip a bunch of files then email them to a customer .
    I have read many strings on the subject and the blow code (kind of) works for me
    // Vars
    IntegerValue	Integer		
    i	Integer		
    Shell32	Automation	'Microsoft Shell Controls And Automation'.Shell	
    SrcFolder	Automation	'Microsoft Shell Controls And Automation'.Folder	
    DstFolder	Automation	'Microsoft Shell Controls And Automation'.Folder	
    SrcItems	Automation	'Microsoft Shell Controls And Automation'.FolderItems3	
    TextFile	File		
    ZipFileName	Text		1024
    SourceFolderName	Text		1024
    //Code
    ZipFileName := 'c:\test.zip';
    SourceFolderName := 'c:\test';
    TextFile.CREATE(ZipFileName); 
    TextFile.TEXTMODE(FALSE); 
    IntegerValue:=101010256; 
    TextFile.WRITE(IntegerValue); 
    IntegerValue:=0; 
    FOR i:=1 TO 9 DO 
      TextFile.WRITE(IntegerValue); 
    TextFile.CLOSE; 
    if isclear(Shell32) then
      CREATE(Shell32);
    
    SrcFolder:=Shell32.NameSpace(SourceFolderName); 
    DstFolder:=Shell32.NameSpace(ZipFileName); 
    SrcItems:=SrcFolder.Items; 
    DstFolder.CopyHere(SrcItems); 
    

    but im an inquisitive so and so... therefore i have a few questions and would like to take the process to another level of complexity
    first my questions
    1.What is the relevance of
    IntegerValue:=101010256;
    2.Whats the point of the loop?
    FOR i:=1 TO 9 DO

    Now for the complexity
    If (as i suspect) the loop is simply a filler of time to allow for the zip process to work, then how would I develop the code to allow for a variable number of files. For example I am trying to compress our Proof Of Delivery .tif files per customer and Email them to the customer. One customer folder might contain 1800 documents the next may only hold 1. so putting a static loop of 9 may delay the process where it doesnt need to be delayed.

    A further complexity issue relates to the email section of the process. Due to email poliocy, the biggest email attachment i can send is 3 Meg. So how would i split this zipping process to create a new zip file each time the file exceeds the 3MB size limit??

    As always, thanks in advance

    M@
    M@
    I have seen the future and it's egg shaped.
  • jhenavjhenav Member Posts: 6
    Anyone knows something about Password? :/
  • euekimeuekim Member Posts: 21
    jhenav wrote:
    Anyone knows something about Password? :/

    this is an old story.. hope u fixed it.. if not hope this will help u

    http://www.mibuso.com/dlinfo.asp?FileID=1445
    Thinking, programming, coding, developing == Music!
  • sanjeevasawalesanjeevasawale Member Posts: 63
    euekim wrote:
    jhenav wrote:
    Anyone knows something about Password? :/

    this is an old story.. hope u fixed it.. if not hope this will help u

    http://www.mibuso.com/dlinfo.asp?FileID=1445

    Hi,

    URL given above is dependent on 3rd party tool. Instead of that with the existing 'Microsoft Shell Controls And Automation' is it possible to use Password?

    Thanks in advance.

    Regards,

    Sanjeev
  • euekimeuekim Member Posts: 21
    yes u can used it to zip a file with password.

    hope it helps
    Thinking, programming, coding, developing == Music!
Sign In or Register to comment.