Jump to content
Benoit

.net decompression GZip or deflate

Recommended Posts

Hi everyone,

I am struggling in one weird behaviors concerning .net in LabVIEW.

 Why as the picture bellow show, I cannot select the method of compression/decompression?

In LabVIEW 2011 this method works as expected. It seems that the constructor changed since the old .net. but for some reason, I cannot decompress any string with any software I did with my LabVIEW 2017.

 

Anyone know how to do that or understand what is going on?

 

PLEASE!!! do not suggest me external tools. this is a requirements to use the native .net in Windows 10... Security reason... I cannot use external .dll and I have a s**t load of data in our database that cannot be accesses anymore. So I need to use the native .net.

 

Thanks everyone for your support.

 

Benoit

GzipStream no compression mode appear.jpg

Share this post


Link to post
Share on other sites

You could try going back to the older .net version:

https://knowledge.ni.com/KnowledgeArticleDetails?id=kA00Z0000019LDcSAM

The Gzipstream msdn doc says:

Starting with the .NET Framework 4.5, the DeflateStream [which~=Gzipstream] class uses the zlib library for compression. As a result, it provides a better compression algorithm and, in most cases, a smaller compressed file than it provides in earlier versions of the .NET Framework.

 

I'm assuming that a zip is a zip and the algorithm doesn't matter, but maybe something else changed then as well.

Share this post


Link to post
Share on other sites

While I know this topic is a bit old now, I just wanted to point you to this thread that I created a while back.

The behavior you're observing is a known bug in LabVIEW that currently doesn't have a solution - just workarounds that all relies on external components ?

  • Sad 1

Share this post


Link to post
Share on other sites

Thanks,

But if you read this thread, you will notice that I posted already.

I cannot use external code from anyone outside... possible security threat. We had no choice to do as you said anyway. The problem with NI is that they prefer to release new stuff with bug than fixing the old one since they believe that there is a work around... ?

Benoit

Share this post


Link to post
Share on other sites

Don't always believe what others say :-) When the .NET support of LV doesn't suit your needs use .NET reflection. This way you can get exactly the constructor you want. Here is some code to show the idea:

CreateGZipStream.png

GZipStream.zip

Share this post


Link to post
Share on other sites

OK I see code, but I see no explanation. I see no way to get the string in and out. As you may notice, I am not good at .net.

If your solution is really working, Please add some comments so I can understand it.

Moreover, there is plenty of people that are struggling to find a way out. If there is, like you said, please share your knowledge.

Thanks

Benoit

Share this post


Link to post
Share on other sites

The inputs and outputs are streams. You can convert them to strings.

Share this post


Link to post
Share on other sites

Benoit, sorry about the missing documentation. The snippet above is just the constructor call

new GZipStream(Stream stream, CompressionLevel compressionLevel)

expressed via .NET reflection. Yes, it is that complicated but at least it's possible.

For a more complete example see the ZIP file appended to my first post. I will write a more detailed explanation tomorrow.

Share this post


Link to post
Share on other sites

It will look very basic my question, but it's because my understanding of the .net is very basic. :D

what do you mean by reflection?

I'm waiting impatiently for your example with more documentation.

Benoit

Share this post


Link to post
Share on other sites

https://dotnetcademy.net/Learn/4/Pages/1

Its roughly analogous to combination of VI server/scripting + lvoop class loading + variant data type inspection VIs + openg type code inspection VIs...except with a more consistent design

His point is just "if you can't call the constructor directly because labview is broken, you can have .net call the constructor manually, by name, using reflection". The direct labview equivalent of what he posted is https://zone.ni.com/reference/en-XX/help/371361P-01/glang/get_lv_class_default_value/

Edited by smithd

Share this post


Link to post
Share on other sites
17 hours ago, smithd said:

https://dotnetcademy.net/Learn/4/Pages/1

Its roughly analogous to combination of VI server/scripting + lvoop class loading + variant data type inspection VIs + openg type code inspection VIs...except with a more consistent design

Great article, thanks! And a pretty cool way to explain things in LV terms:thumbup1:

 

OK, here the detailed explanation as promised:

You can find a very simple example in the ZIP file attached to my first post, GZipStream.Example.vi . There's a file Hello.txt, if you run GZipStream.Example.vi you'll get the compressed file Hello.txt.gz . To decompress it, activate the other frame of the diagram disable structure and run it. The output will be Hello2.txt . There's not much to say about this example VI, the code consists of just .NET invoke nodes and properties.

The code of CreateGZIPStream.vi is that what matters here. We use .NET reflection to call a constructor that would be otherwise inaccessable for LV. Reflection is the ability to query a .NET assembly (DLL or EXE) for the classes (and other types) contained within and ask these classes for their properties, methods, events and constructors. It is built into the .NET framework and thus always at your service if the .NET support of LV isn't enough. The most important .NET data type when using reflection is System.Type .

OK, let's now dissect the code. Sorry about using C# here but I know no better way to explain .NET code.

This is what we want to do:

GZipStream CreateGZipStream(Stream stream, CompressionMode compressionMode) {

 // Call the constructor.

 return new GZipStream(stream, compressionMode);

}

Since LV doesn't allow us to select this constructor we need another way to get and invoke it. Reflection makes that possible, although the code becomes much more complex. CreateGZipStream.vi is roughly equivalent to the following C# code:

GZipStream CreateGZipStream(Stream stream, CompressionMode compressionMode) {

// Get the types.

Type streamType = typeof(Stream);

Type compressionModeType = typeof(CompressionMode);

Type gzipStreamType = typeof(GZipStream);

// To get the desired constructor we need its argument types and bundle them into an array.

Type[] constructorArgTypes = new Type[] { streamType, compressionModeType };

// Get the constructor.

ConstructorInfo gzipStreamConstructor = gzipStreamType.GetConstructor(constructorArgTypes);

// Bundle the constructor arguments into an array.

object[] constructorArgs =new object[] { stream, compressionMode };

// Call the constructor and cast the result.

return gzipStreamConstructor.Invoke(constructorArgs) as GZipStream;

}

Unfortunately LV complicates things a little further: We have no direct equivalent of typeof(). We can work around that by either using GetType() or loading the type from the assembly by name. Using GetType() is easier but it requires a valid .NET reference. Thus we need both:

GZipStream CreateGZipStream(Stream stream, CompressionMode compressionMode) {

 // Get the types.

 Type streamType = stream.GetType();

 Type compressionModeType = compressionMode.GetType();

 Type gzipStreamType = compressionModeType.Assembly.GetType("System.IO.Compression.GZIPStream");

 // To get the desired constructor we need its argument types and bundle them into an array.

 Type[] constructorArgTypes =new Type[] { streamType, compressionModeType };

 // Get the constructor.

 ConstructorInfo gzipStreamConstructor = gzipStreamType.GetConstructor(constructorArgTypes);

 // Bundle the constructor arguments into an array.

 object[] constructorArgs =new object[] { stream, compressionMode };

 // Call the constructor and cast the result.

 return gzipStreamConstructor.Invoke(constructorArgs) as GZipStream;

}

But that's still not enough. For LV CompressionMode is an enum, in .NET an enum is a full featured object. So we have also to construct a CompressionMode .NET object in LV and set its enum value. There's no direct equivalent to the 'value__' property in C#, I guess LV does something special here. Now we're ready to invoke the constructor in LV. The snippet again:

CreateGZipStream.thumb.png.ce1f851e5a2f70b065f73a1ac391292b.png

We construct the CompressionMode object and use its Assembly property to get the GZIPStream Type object. Then we ask for the constructor using the parameter types bundled into an array. Finally we call that constructor with the arguments bundled into an array and cast the result to GZipStream.

A note about the style of the code: When it comes to external reference-based code I use a single error wire through all nodes to enforce some kind of control flow. Furthermore it's important to close all open .NET references after using them.

I'm using .NET reflection to do things that are not supported by LV for a couple of years now, in my case .NET Remoting with events, see here (no reflective constructor invocation, though).

 

 

 

 

 

  • Like 1
  • Thanks 1

Share this post


Link to post
Share on other sites

?

OK I'll try to digest that. I'll need a couple of days.

But thank you very much.

Even that I can use your example and make something working right now, I still do not understand it enough to create it by myself from scratch.

But I'll get to that point very soon.

Thanks again.

I leave all of you with an unanswered question... Why NI do not provide this as a workaround? They always said create an external .dll to access .net function...

Benoit

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.