|
Basicly I want to be able to compress a String using System.IO.Compression to another String and be able to decompress it later.
I want to use this for compressing XML (and other string values) into a String variable which I store elsewhere. It MUST be compressed to a String - no binary data.
Also, I do not want to save the XML to a file and compress/decompress from there; compressing/decompressing must be in-memory only.
Any advice or sample code (VB.NET or C#) ?
Thanks in advance
|
|
|
|
|
You do realize that the Compression class only outputs binary data? Outputs to any stream by the way, even a MemoryStream object. You can find an example here[^].
RageInTheMachine9532
"...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome
|
|
|
|
|
"You do realize that the Compression class only outputs binary data?"
Yes, but this can be converted (encoded) to/from a String with .NET (can't remember the class name right now).
Also, I'm NOT looking for something to compress a stream...
Anybody else, please ?
|
|
|
|
|
MrBean wrote: Also, I'm NOT looking for something to compress a stream...
Actually, you are! Consider the String as just a stream of characters in memory. See this[^] for more information.
MrBean wrote: Yes, but this can be converted (encoded) to/from a String with .NET (can't remember the class name right now).
You could probably do what you want with a StringBuilder[^] object.
RageInTheMachine9532
"...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome
|
|
|
|
|
Dave Kreskowiak wrote: Actually, you are! Consider the String as just a stream of characters in memory. See this[^] for more information.
Yes, you are right - but I'm looking for some code which shows me how to do it in more detail
I got the compression code to work just fine, but can't seem to decompress it.
Here's the compression code which seems to work :
Public Function CompressString(ByVal strInput As String) As String
Dim buffer() As Byte = Encoding.UTF8.GetBytes(strInput)
Dim memoryBuffer As New MemoryStream()
Dim compressedZipStream As New GZipStream(memoryBuffer, CompressionMode.Compress, False)
compressedZipStream.Write(buffer, 0, buffer.Length)
compressedZipStream.Close()
Return Convert.ToBase64String(memoryBuffer.GetBuffer)
End Function
Dave Kreskowiak wrote: You could probably do what you want with a StringBuilder[^] object.
Ok, but Convert.ToBase64String seems to work also
|
|
|
|
|
OK. That seems to work. Now how about the decompression code? It should be doing the exact same thing in reverse...
RageInTheMachine9532
"...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome
|
|
|
|
|
Well... can't get it to work
Below is the code that I'm currently "fiddling" with.
The CompressString function has been revised and seems to work correctly now. But the Decompress function method somehow still doesn't work.
ZipStream.Read simply doesn't decompress the data in the memoryStream and returns 0 ??
Public Function CompressString(ByVal strInput As String) As String
Dim bufIn() As Byte = Encoding.UTF8.GetBytes(strInput)
Dim bufIn() As Byte = Encoding.UTF8.GetBytes(strInput)
Dim ms As New MemoryStream()
Dim ZipStream As New GZipStream(ms, CompressionMode.Compress, False)
ZipStream.Write(bufIn, 0, bufIn.Length)
Dim bufOut() As Byte = ms.GetBuffer
ZipStream.Close()
Return Convert.ToBase64String(bufOut)
End Function
Public Function DeCompressString(ByVal strInput As String) As String
Dim bufIn() As Byte = Convert.FromBase64String(strInput)
Dim ms As New MemoryStream()
ms.Write(bufIn, 0, bufIn.Length) ' Compressed data
ms.Position = 0
Dim ZipStream As New GZipStream(ms, CompressionMode.Decompress)
Dim bufOut(30000) As Byte ' Predefined buffer-size just for test purposes!!
Dim nRead As Integer
nRead = ZipStream.Read(bufOut, 0, bufOut.Length) ' Returns 0 ????
ZipStream.Close()
Return Convert.ToString(bufOut) ' never mind this... WIP
End Function
|
|
|
|
|
This code should do the trick:
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.IO.Compression;
namespace CompressDecompress
{
class Program
{
static void Main(string[] args)
{
string LstrTest = "This is a test";
string LstrOutput;
LstrOutput = CompressString(LstrTest);
LstrTest = DeCompressString(LstrOutput);
Console.WriteLine(LstrTest);
}
public static string CompressString(string PstrInput)
{
MemoryStream Lms;
GZipStream LstmZipStream;
byte[] LbytBufIn;
byte[] LbytBufOut;
LbytBufIn = Encoding.UTF8.GetBytes(PstrInput);
Lms = new MemoryStream();
LstmZipStream = new GZipStream(Lms, CompressionMode.Compress, false);
LstmZipStream.Write(LbytBufIn, 0, LbytBufIn.Length);
LbytBufOut = Lms.GetBuffer();
LstmZipStream.Close();
return Convert.ToBase64String(LbytBufOut);
}
public static string DeCompressString(string PstrInput)
{
MemoryStream Lms;
GZipStream LstmZipStream;
UTF8Encoding Lutf;
byte[] LbytBufIn;
byte[] LbytBufOut;
int LintRead;
LbytBufIn = Convert.FromBase64String(PstrInput);
Lms = new MemoryStream();
Lms.Write(LbytBufIn, 0 , LbytBufIn.Length);
Lms.Position = 0;
LbytBufOut = new byte[LbytBufIn.Length];
LstmZipStream = new GZipStream(Lms, CompressionMode.Decompress);
LintRead = LstmZipStream.Read(LbytBufOut, 0, LbytBufOut.Length);
LstmZipStream.Close();
Lutf = new UTF8Encoding();
return Lutf.GetString(LbytBufOut, 0 , LintRead);
}
}
}
|
|
|
|
|
Thanks for the code... I'll try it later
So far I have solved my problems by using a 3rd party component (Xceed) which was part of a suite we had allready purchased.
It workes perfectly, is very easy to use and is apparently more efficient
|
|
|
|
|
hi all,
i am new to vb.net. how can we add Microsoft common dialog control 6.0(sp3) to vb.net project
raj
|
|
|
|
|
Why would you want to when the dialogs are already in the .NET Framework?
OpenFileDialog, SaveFileDialog, ColorDialog, FolderBrowserDialog, FontDialog, PageSetupDialog, PrintDialog, ...
RageInTheMachine9532
"...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome
|
|
|
|
|
I create a document by using a class and inheriting from PrintDocument. On the PrintPage event i keep adding new pages etc. This part is succesfull so far because i then view the document in the print preview dialog all 34 pages.
However when i attempt to print the document by clicking on the print button of the dialog control ONLY THE LAST PAGE GETS SENT to the printer.
I have played around with printersettings option but to no avail.
If you can think of why i have this problem please let me know,
i would be very grateful if you could help.
Thanks.
Mo.
|
|
|
|
|
Is there any way to get a parent control to accept messages before a child control does? For instance, I have a richtextbox inside of a control that is inside of my current control that seems to be grabbing my OnDrag... messages. I want any of the OnDrag... functions to be handled by my control first and then passed to the child controls to be handled by them. Heck, it would even be good if the child control handled it first and then it percolated outwards to my parent control. That is not happening, however. What is happening is that my dragdrop is being prevented by the inner child control, which ruins the functionality of my outermost parent control.
Is what I'm trying to do possible?
|
|
|
|
|
Hi,
I'm involved in a project which will create a few decision records on how to manage / deploy .NET applications in our environment.
Besides the ".NET best practices" whitepapers from Microsoft, is there any other place I can get some more information about a general .NET architecture or implementation??
Is there any "best practice" information for managing .NET infrastructure (as opposed to developing it)?
thanks for any advice or suggestions!
|
|
|
|
|
Hi,
can anyone direct me to an article which describes importing CSV data into few different variables?
|
|
|
|
|
What language? Some sample in C#, it will read this content:
value1, value2, value3, value4, value5<br />
...
StreamReader reader = new StreamReader(file);
string line = "";
Regex lineMatch = new Regex("([^,]+),([^,]+),([^,]+),([^,]+),([^,]+)");
Match matches = null;
while((line = reader.ReadLine()) != null)
{
matches = lineMatch.Match(line);
if(matches.Success)
{
}
}
regards
modified 12-Sep-18 21:01pm.
|
|
|
|
|
yes, language is c#, and this code solves problem...thanks.
|
|
|
|
|
Hello,
When I am uninstalling a setup project, some files (that are not added there at installation time, but are created later by my program) are not deleted. I want to be able to delete those files, too. I tried adding a custom uninstall action where I am specifying an .exe to execute. The exe deletes the two files when is executed, but this does not happens during the uninstall process. Any ideas?
Thanks
|
|
|
|
|
Hi All,
I have a doubt on the Out Of Memory Exception thrown by CLR!! Why does it do so, When Windows is providing a virtual memory of 4GB for a 32-bit processor for each and every process!! So according to this if an application starts, then a virtual memory of 4-GB is provided to it, and Windows keeps performing complex algorithms to swap data from RAM to physical memory!!! My doubt is when Windows is doing so much why at all we get an Out of Memory Exception??
I beleive that the memory management is a lot more complex than i have understood, can any of you please enlighten me on the same??
Thanks anyway,
Rajesh
|
|
|
|
|
OutOfMemoryExceptions can occur for other reasons, if I remember correctly. For example, GDI resources that don't get cleaned up properly will eventually run out. So, if you create an instance of a class that supports the IDisposable interface then remember to Dispose() it when you are done with it.
ColinMackay.net
"Man who stand on hill with mouth open will wait long time for roast duck to drop in." -- Confucius
"If a man empties his purse into his head, no man can take it away from him, for an investment in knowledge pays the best interest." -- Joseph E. O'Donnell
|
|
|
|
|
Colin is correct. If you use any GDI or GDI+ objects, especially when doing you own custom drawing, and don't properly release them by calling Dispose when your done with them, the unmanaged underlying system handles don't get released and are eventually exhausted, causing your OutOfMemory Exception.
There are other things that will cause this too, but not properly Disposing the objects you're using, no matter what they are, will result in exhausting one or more of the system handle pools.
If you want a quick check of this, open TaskManager, click on its Processes tab, then go to the view menu and pick Select Columns. Turn on Handles, Threads, USER Objects, and GDI Objects. Click OK, then go to the list and file your app. Watch it run for a little while. If any of these counters just keeps rising the longer you use your app, this gives you a hint of where the problem lies.
Handle counts usually don't rise above 1,000 in most cases. I have seen examples of piss-poor programming (in commercial software, no less!) where the Handle count grew above 100,000 handles in use! This is a REALLY BIG clue there's leaky code somewhere in that app...
RageInTheMachine9532
"...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome
|
|
|
|
|
Hi,
I need to serialize an unmanaged class in VC .NET. Please guide me how can I do this.
Ahmad
|
|
|
|
|
i have created a console application in C#.Net. This exe calls a dts package on a sql server and that package in return generates a text file and some excel files as result.
i have scheduled this through window scheduled tasks option to run daily with a domain account which is not included in the adminstrator group of the local machine.
The problem is that the text file is generated but the excel files are not.
But when i run the exe thru : right click> Run As> and then provide the same non admin credentials, all files including excel files are generated.
Even when i log on to the system as this non admin account and run the exe directly, i get full results!
Is there any privilege difference in executing a task thru the task scheculer? or am i missing something?
I have also checked the event viewer and the following error was logged:
"Access denied attempting to launch a DCOM Server. The server is:
{00024500-0000-0000-C000-000000000046}
The user is visiadmin/KARACHI, SID=S-1-5-21-854245398-2146720035-839522115-2639."
visiadmin is the non admin account and KARACHI is the domain name.
i have also followed the support on
http://support.microsoft.com/default.aspx?scid=kb;en-us;290398
but still no results!
I am using VS.Net 2003, O/S: Windows Server 2003, exe developed on C#.NET, MS Office 2003.
Please help me!
thanx in advance
Regards
☺«««DTA»»»☺
|
|
|
|
|
Could someone please tell me if firefox is compatible with .NET framework? I need to write a firefox plugin to call a particular engine that is written in c++ , so..
|
|
|
|
|
Firefox plugins are written in javascript. If you can get a JS plugin to start a process with your engine (and your computer has the framework) then sure.
I looked at the CVS For IeView a firefox plugin that views a page in ie... So its possible to start a process...
http://www.mozdev.org/source/browse/ieview/ieview/content/ieviewOverlay.js[^]
<br />
usePath = targetFile.target;<br />
<br />
var process = Components.classes['@mozilla.org/process/util;1'].getService(Components.interfaces.nsIProcess);<br />
process.init(targetFile);<br />
var arguments= [] ;<br />
<br />
arguments.push(argumentstext);<br />
<br />
process.run(false, arguments, arguments.length, {});<br />
return true;<br />
All I can suggest is Googleing Components.classes and @mozilla.org/process/util
I'll get ya started:
http://www.xulplanet.com/references/xpcomref/comps/c_processutil1.html[^]
Matthew Hazlett
Sometimes I miss the simpler DOS days of Borland Turbo Pascal (but not very often).
-- modified at 20:35 Wednesday 25th January, 2006
|
|
|
|
|