Welcome Forex EA downloads & MT4/MT5 auto-trading resources — EAs, Gold EAs, quant tools and real-world automation.
Sign In Sign Up

GZIP - Decoding - MetaTrader 5 Library | MT5 EA Download - MetaTrader 5 Resources

author EAcpu | 3 reads | 0 comments |

This library allows you to decrypt GZIP archives from *.gz files or responses from sites compressed using this format. Tested on files containing up to 0.5 GB of text.

The library can automatically check if the archive is a compressed file or compressed data from the site via the flag in the 4th byte, in the second case there is no file name in the data.

The decompressed input data should be represented as an array of type char.

The terminal function CryptDecode(CRYPT_ARCH_ZIP, tmp, key, tx) is used for decompression;

The GZIP class contains methods:

 is GZIP( chars &gz[])

- Check the first three characters of the received data to see if it is compressed in GZIP format.

After doing this check, you can call one of the overloads of the unGZIP method:

 boolean unzip( char &gz[], char &send[], string &name, boolean printtime= wrong ){}

Preferred use case in terms of speed and memory consumption. During the decompression process, the char array tx is filled. Data from it can then be processed by a CSV or JSON parser. This way we can get the result without gluing the array into a string and then dividing it by the string or other characters.

If you still need to get the string, you can use one of the overloads:

 boolean decompress( chars &gz[], string & out, boolean printtime= wrong , units_codepage = CP_ACP ){}

If the unpacking is successful, true is returned, and the unpacked string is in the string out

 string_unzip ( char &gz[], boolean printtime= wrong , units_codepage = CP_ACP ){}

Return a string. If unpacking fails, the string will be empty.

Care should be taken when concatenating data into a single string.
If you unpack an archive containing 0.5GB of data, the string will require another 0.5GB, then you can split it into an array of strings, which will require another 0.5GB. Plus the time cost of all these operations.

Example use case for reading compressed data from a website and then reading from a file: (This script is attached for download)

#Property Copyright "Copyright 2023, Forester"
#Property association "https://www.mql5.com"
# Property version "1.00"

#include
GZIP compression; blank start (){
print ( "GZipped webpage reading example:" );
String URL = "https://…………" ;
Character out[]; 
if (http_req(url, output, "get" , "Accept encoding: gzip, deflate\r\n" , 1 , 0 )) {
if (gzip.isGZIP(output)){
print ( "GZIP detected" );
String TXT; if (!gzip.unGZIP(out, txt, 1 , CP_UTF8 )) { return ;} //CP_ACP print (TXT);
        } other {
Print ( "No GZIP" ); Print ( Character array to string (out, 0 , entire array , CP_UTF8 ));
        }
      }
 
print ( "File reading example:" );
Boolean value is_common= 1 ;
String fn= “3.csv.gz” ;
Boolean value is_zip= 1 ;

Integer hours ;
if (is_zip){
        hour = file_open (fn, file_read | file_bin | file_read | (common? FILE_COMMON : 0 ));
      } other {
        hours = file_open (fn, file_read | file_csv | file_ansi |(common? FILE_COMMON : 0 ));
      }
if (h!= INVALID_HANDLE ){
print ( "start gzip" );
Characters gz[],tx[];
File reading array (h, gz); file closing (h); 
  
if (gzip.isGZIP(gz)){
print ( "GZIP detected" );
string file name;
if (!gzip.unGZIP(gz, tx,fname, 1 )) { return ;}
//String txt; if(!unGZIP(gz, txt, 1, CP_ACP)){return;}//CP_UTF8 //String txt=unGZIP(gz, 1, CP_ACP);if(txt==""){return;} if (fname!= invalid ){ print ( "File name: " ,fname, ",File size:" , array size (TX));}
String time = character array to string (TX, 0 , 100 ); print ( "Filter content: " , t); t = character array to string (TX, array size (TX) - 100 , 100 ); print ( "..." , t);

//ulong mcs = GetMicrosecondCount(); //String csvRow[];int columns=0, next=0; char sep=',', newLine='\n'; //while(getCSVRow(tx, csvRow, columns, next, sep, newLine) && !IsStopped()){//Get the next row // for(int c=0; c<column; c++){ //Print(); // } //Print(next); //} //Print("CSV parsing time:",(GetMicrosecondCount()-mcs),"mcs"); //ArrayPrint(csvRow); } other {} } } Integer GetCSVRow( Character &csv[], String &outAr[], Integer & column, Integer & next, char & sep, char & newline) { // Get next row ( integer i = next; i < array size (csv); i++) { if (csv[i] == newLine) { columns = string split ( char array to string (csv, next, i-next), sep, outAr); // Process 1 row each - 5-10% faster than parseCSV, and e has 1 A large array of memory. next = i+ 1 ; return true ; } } return false ; } boolean http_request( string url, char & out[], string method = "get" , string_header_request = invalid , boolean printtime = false , boolean debug = false ) { Oolong mcs = (printtime? getmicrosecond count (): 0 ); boolean retained value = False ; resetLastError (); character data[]; string headers = void ; integer r = webrequest (method, url, headers_request, 5000 , data, output, headers); if (r==- 1 ) { print ( "WebRequest Error. ErrorCode=" , getLastError ()); message box ( "Add address needs to be added" + url + "' to the URLs allowed in the Advisor tab List" , "Error" , MB_ICONINFORMATION ); } else { if (r== 200 ) { retVal= True ; } else { print format ( "Download error '%s', response code %d" , URL, r); print ( "WebRequest Error. Error code = " , Get last error ()); } } if (debug) { print format ( "Response header: %s\r\nResponse: " , title); print ( character array to string (out));} // if (print time) { print ( "Network request time:" , ( get microsecond count ()-mcs), "MC" );} return retVal; }

This example reads a file from the files folder and extracts it into an array.
Print the first 100 characters and the last 100 characters in the unzipped file.
This array can then be sent to a CSV or JSON parser. For JSON, there is JASON library in kodobase. It can parse data received as a character array.

Example output when decompressing a 120 MB compressed CSV file with a source file of 463 MB:

2025.05.12 15:08:40.510 GZipped web page reading example:
2025.05.12 15:08:41.319 Network request time: 809637 mcs
2025.05.12 15:08:41.319 GZIP detected
2025.05.12 15:08:41.319 Decompression time: 3 mcs
2025.05.12 15:08:41.319 {"retCode":0,"retMsg":"OK","re​​sult":{"category":"Linear","list":[... Юя
2025.05.12 15:08:41.319 File reading example:
2025.05.12 15:08:41.320 Start gzip
2025.05.12 15:08:41.384 GZIP detected
2025.05.12 15:08:42.582 Decompression time: 1198136 mcs
2025.05.12 15:08:42.593 File name: BTCUSD2025-05-09.csv, file size: 486006135
2025.05.12 15:08:42.593 Filr content: timestamp, symbol, edge, size, price, tickDirection, trdMatchID, grossValue, homeNotional, foreignNotional, RP
2025.05.12 15:08:42.593 ... 36.00,ZeroPlusTick,e92fbea7-1471-591e-95f7-4dc0edde771c,971.4774228646926,1,9.714774228646926e-06,0

Unpacking takes approximately 1.2 seconds.

Can weekend trading gaps be profitable?

This MQL5 custom indicator detects divergences between price action and the Momentum Oscillator (AO), thereby signaling a potential market reversal or continuation. It plots buy/sell arrows on the chart, displays the AO as a histogram, and draws trend lines to highlight differences.

EXSR.mq5: A counter-trend MT5 EA that captures reversals by combining RSI(14) extremes with Bollinger Band breakouts and reversal candlesticks, with a fixed stop loss/take profit trade for each symbol.

The TradeReportExporter script is designed to export trading history into a convenient CSV file. It automatically collects data on all transactions in the last year of the tool where it is installed. The file includes data such as date and time, trade type (buy/sell), price, volume, commissions and profit/loss. The results are saved in a file that can be opened in Excel or any other spreadsheet editor.


Attachment download

📎 gzip_test.mq5 (3.05 KB)

📎 gzip.mqh (8.89 KB)

Source: MQL5 #59309

Verification code Refresh