Zip and Unzip files ?

 

Hi,

Is it possible to zip/unzip a text file from a mql4 code ?

I think we could use kind of zip dll but if someone already did it, it would be great :)

Thanks a lot,

Manu

 
There is a free zip library on the net but it seem haven't been update for a long time. It is very popular in old days. If you are interested, here is the link http://zlib.net/
 

Very interested in this if anybody has found a solution. I am trying to create a script which goes to the CTFC website downloads a the .csv and then puts it into a specific folder. My problem is that I can't use MoveFileW() for the .csv file in the zip folder. 

If anybody has found a solution please let me know. 


Many Thanks


#define READURL_BUFFER_SIZE   1000000

#import  "Wininet.dll"
   int InternetOpenW(string, int, string, string, int);
   int InternetConnectW(int, string, int, string, string, int, int, int); 
   int HttpOpenRequestW(int, string, string, int, string, int, string, int); 
   int InternetOpenUrlW(int, string, string, int, int, int);
   int InternetReadFile(int, uchar & arr[], int, int& OneInt[]);
   int InternetCloseHandle(int); 
#import

#import "shell32.dll"
   int ShellExecuteW(int hWnd, string Verb, string File, string Parameter, string Path, int ShowCommand);
#import 

#import "kernel32.dll"
   bool CopyFileW(string lpExistingFileName, string lpNewFileName, bool failIfExists); 
   bool MoveFileW(string lpExistingFileName, string lpNewFileName);

#import
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+

void OnChartEvent(const int id,const long &lparam,const double &dparam,const string &sparam)
{
   if(id==CHARTEVENT_KEYDOWN)
      
      if(lparam == 81)
         {
            geturl("http://www.cftc.gov/files/dea/history/fut_disagg_txt_2017.zip");
         }
      else if(lparam == 87)
         {
            {
               MoveFileW("C:\\Users\\alan_\\Downloads\\fut_disagg_txt_2017\\f_year.txt", "C:\\Users\\alan_\\Documents\\f_year.txt");
              
            }
         }
}

//+------------------------------------------------------------------+
string geturl(string url)
   {  
   int HttpOpen = InternetOpenW(" ", 0, " ", " ", 0); 
   int HttpConnect = InternetConnectW(HttpOpen, "", 80, "", "", 3, 0, 1); 
   int HttpRequest = InternetOpenUrlW(HttpOpen, url, NULL, 0, 0, 0);
   
   int read[1];
   uchar Buffer[];
   ArrayResize(Buffer, READURL_BUFFER_SIZE + 1);
   string page = "";
   while (true)
      {
      Print("While loop is ongoing");
      InternetReadFile(HttpRequest, Buffer, READURL_BUFFER_SIZE, read);
      string strThisRead = CharArrayToString(Buffer, 0, read[0], CP_UTF8);
      if (read[0] > 0)
         page = page + strThisRead;
      else
      break;
      }
      
   int fileHandle = FileOpen("Data//Test.txt", FILE_WRITE|FILE_TXT);
   
      if(fileHandle != INVALID_HANDLE)
         {
            PrintFormat("File path: %s\\Files\\",TerminalInfoString(TERMINAL_DATA_PATH));
            FileWriteString(fileHandle, page+"\r\n");
            FileClose(fileHandle);
            Print("File successfully created");
         }
 
   if (HttpRequest > 0) InternetCloseHandle(HttpRequest);

   if (HttpConnect > 0) InternetCloseHandle(HttpConnect);
   
   if (HttpOpen > 0) InternetCloseHandle(HttpOpen);  
   
   return page;
}
 
  1. What about wget (started by the Task Scheduler) to dld the website and place it in the folder you want?
  2. What about PowerShell started by the Task Scheduler theat uses wget for the dld and then parses the file you have got...
 
Manu:

Hi,

Is it possible to zip/unzip a text file from a mql4 code ?

I think we could use kind of zip dll but if someone already did it, it would be great :)

Thanks a lot,

Manu

What about

#import "shell32.dll"
// https://www.mql5.com/en/articles/1467
/* Normally we should define ShellExecute like this:
int ShellExecuteW(int hWnd,string lpVerb,string lpFile,string lpParameters,string lpDirectory,int nCmdShow);
But, we need to keep lpVerb,lpParameters and lpDirectory as NULL pointer to this function*/
//---- So we need to define it as (look: "string" parameters defined as "int" to keep them NULL):
// usage      ShellExecuteA(0,0,"notepad.exe", 0,0,SW_SHOW); http://www.forexfactory.com/showthread.php?t=7145&highlight=shell
// or:  RIS = ShellExecuteA(0,0,"yourfile.doc",0,"C:'\'Documents and Settings'\'user22'\'Desktop'\'",5);
// or:  int r=ShellExecuteA(0, OPERATION, file, parameters, DEFDIRECTORY, SW_SHOW);
//      if (r <= 32){   Alert("Shell failed: ", r); return(false);  }
//              string ps = "C:\\Users\\cas\\Documents\\sysTools\\PC\\WindowsPowerShell\\emailDaxacan";
//              string sc = "-NoExit -file loadPOP3emails.ps1";
//              int ret = ShellExecuteW(0,0,"powershell.exe",sc,ps,SW_SHOW);
//              if (ret <= 32) Alert(ret);

int ShellExecuteW(int hWnd,int lpVerb,string lpFile,string lpParameters,string lpDirectory,int nCmdShow);
#import
and start either zip.exe or WinZip?
 
Manu: Is it possible to zip/unzip a text file from a mql4 code ?
  1. Would you expect Excel to do video editing? Then don't expect a trading platform to handle foreign file types. Shell it.
  2. 7-zip can handle zip, gzip, rar, etc., is free, and has a command line interface as well as a GUI.
 

This solution does not suit you?

https://www.mql5.com/en/articles/1971

Handling ZIP Archives in Pure MQL5
Handling ZIP Archives in Pure MQL5
  • 2015.10.29
  • Vasiliy Sokolov
  • www.mql5.com
The MQL5 language keeps evolving, and its new features for working with data are constantly being added. Due to innovation it has recently become possible to operate with ZIP archives using regular MQL5 tools without getting third party DLL libraries involved. This article focuses on how this is done and provides the CZip class, which is a universal tool for reading, creating and modifying ZIP archives, as an example.