Overview
This example's functions will compress and decompress a LabVIEW string using the .NET framework.
Description
The inputs and outputs are strings, there is no file I/O involved. Applications include large strings transfer over slow connections or storing large strings (XML) in a database BLOB.
For the compression VI:
1. Call the constructor of the MemoryStream Class located in mscorlib 2.0.0.0 >> System.IO >> MemoryStream to create a stream whose backing store is memory
2. Call the constructor of GZipStream Class that provides methods and properties used to compress and decompress streams.
3. Use the GZipStream Invoke methods to write the data to be compressed
4. Use the MemoryStream Invoke Method to retrieve the compressed data
For the decompression VI
1. Call the constructor of the MemoryStream Class located in mscorlib 2.0.0.0 >> System.IO >> MemoryStream to create a stream whose backing store is memory
2. Call the constructor of GZipStream Class that provides methods and properties used to compress and decompress streams in Decompress mode
3. Use the GZipStream Invoke methods to read the decompressed data
( dotNET Decompress String LV2014 v2.vi added based on technique defined in https://lavag.org/topic/20613-net-decompression-gzip-or-deflate/ )
Requirements
LabVIEW 2012 (or compatible)
.Net Framework 3.5 (or compatible)
Steps to Implement or Execute Code
1. Download the folder "dotNet Compress&Decompress Strings_LV2012_NI Verified"
2. Open the VI "dotNET Compress String"
3. Run the VI
4. Note the compressed string and copy it
5. Open the VI "dotNET Decompress String"
6. Paste the compressed string in GZIP Compressed String
7. Run the VI and note the decompressed string
Additional Information or References
4/1/2015 - Added LV2014 Versions that use .NET 4.0 Assemblies
Block Diagram of Compression VI
Front Panel of compression VI
Block Diagram of decompression VI
Front Panel of decompression VI
**This document has been updated to meet the current required format for the NI Code Exchange.**
Example code from the Example Code Exchange in the NI Community is licensed with the MIT license.
Nice, another one for the toolbox
It seems that the GZIP isn't working with dotnet 4.0.
I have installed Labview 2014 SP 1 with dotnet 4.0 and in the decompressing VI it get this Error:
Invoke Node System.Reflection.TargetInvocationException: Ein Aufrufziel hat einen Ausnahmefehler verursacht.
Inner Exception: System.InvalidOperationException: Das Lesen aus dem Komprimierungsstrom wird nicht unterstützt. <append><b>System.InvalidOperationException</b> in Global_Library.lvlib:gZip Decompress String.vi
Does anyone have a Solution for this Error?
Solved:
With the LV2014 VI's it works!!
Thanks
The Decompress process doesn't work with LV2014.
The Error is: code 1172
source:
Invoke Node System.Reflection.TargetInvocationException: Ein Aufrufziel hat einen Ausnahmefehler verursacht.
(A call aim has caused a special mistake.)
Inner Exception: System.InvalidOperationException: Das Lesen aus dem Komprimierungsstrom wird nicht unterstützt. <append><b>System.InvalidOperationException</b> in dotNET Decompress String.vi->test.vi
(The reading from the compression stream is not supported.)
Does anyone have a Solution for this Error?
System: I use LavVIEW 2014 SP1 64bit in the 14.0.1 Verison
If you are having a problem with .NET 4.0, please see this forum entry. At the bottom is a DLL wrapper than can help with decompress.
There is an CAR (#493662) for this problem.
I downloaded the GZip_compress_decompress.vi from Sev_K (Message 26 from 27 from the link of comment 4) but now I am getting the Error 1386 :
Error 1386 occurred at Constructor Node in GZip_compress_decompress.vi
LabVIEW: The specified .NET classis not available in LabVIEW.
SOLUTION: The GZip_Lib.dll file was missing, so LabVIEW didn't know where to look.
If you are looking for a solution that doesn't require .NET and/or works on RT targets - I have written a couple of VIs which you can find here.
A long standing bug created based on a change in the .NET gzip functionality caused the decompress function to fail.
There are other posts on the NI forums that add a fix by using an intermediate DLL.
Based on a post on the LAVAG forums, the bug can be overcome using .NET reflection. https://lavag.org/topic/20613-net-decompression-gzip-or-deflate/
The example on LAVAG assumes that the user is using files to compress and uncompress. I adapted the .NET reflection functionality to use a MemoryStream and allow the Decompress function to work without the need for an external DLL.
See the attached decompress V2 file