decompression in c# - vb.net

i have compressed an xml file and converted it into base64 format in web page.then i have passed the base64 string to windows application and converted it to unbase64 format.now i want to decompress this string in windows form only.
i have done the following in windows
dim decoded as byte()
decoded=convert.frombase64string(strreturndata) // strreturndata is base64 string
dim decoders as string
decoders = encoding.utf8.getstring(decoded)
now i want to decompress this string using gzip stream class in windows form

If you want to get back the Base64 string, use this.
Try this:
byte[] x = Convert.FromBase64String(decodedString);
string mytext = System.Encoding.utf8.getstring(x, 0, x.length)
If you need to do more compression, you can use Gzip compression and decompression or DeflateStream class.

Related

Encoding an image from a file into base 64 bytes into a memory stream (Amazon AWS)

I'm trying to get a short proof of concept up using Amazon AWS Textract (Documentation).
According to the documentation I should be able to call and pass in a Amazon.Textract.Model.Document
The document can be in Bytes or a S3Object, the documentation reads:
Properties: Bytes - System.IO.MemoryStream
Gets and sets the property Bytes.
A blob of base-64 encoded documents bytes. The maximum size of a document that's provided in a blob of bytes is 5 MB. The document bytes must be in PNG or JPG format.
If you are using an AWS SDK to call Amazon Textract, you might not need to base64-encode image bytes passed using the Bytes field.
So my very simple example is throwing a 400 Bad Request exception from AWS. I'm not sure if i have encoding the image correctly, or if i have some other issue.
Dim bData As Byte()
Dim br As BinaryReader = New BinaryReader(System.IO.File.OpenRead(FileName))
bData = br.ReadBytes(br.BaseStream.Length)
Dim ms As MemoryStream = New MemoryStream(bData, 0, bData.Length)
ms.Write(bData, 0, bData.Length)
Dim Client = New AmazonTextractClient()
Dim Obj = New Model.AnalyzeDocumentRequest
Dim Doc = New Model.Document
Doc.Bytes = ms
Obj.Document = Doc
Dim OCR = Client.AnalyzeDocument(Obj)
Console.WriteLine(OCR.HttpStatusCode)
Console.WriteLine(OCR.ResponseMetadata)
Just a simple little example, new to AWS so i'm not sure if there is an issue with my actual request, or further up in my AWS configuration.

How would do I upload a PDF file?

I am currently making an app for students where they can upload a PDF file to a server. I am using the android Volley API but have been testing the function using JPEG files.
This is my code
public String getStringImage(Bitmap bmp) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.JPEG, 100, baos);
byte[] imageBytes = baos.toByteArray();
String encodedImage = Base64.encodeToString(imageBytes, Base64.DEFAULT);
return encodedImage;
}
How would I change this code so that a PDF can be uploaded instead?
Do I still used Base64? and imageBytes?
Or is there an alternate method?
You should be able to reuse most of the code that you have there, instead of taking in a Bitmap, you would take in a File. You won't be able to use Bitmap.Compress on it though so that will need to be removed.
For uploading a PDF, you can Base64 encode it and send it as a string as part of the request, this might get out of control if you are handling large files.
The other option is to use a multipart form, I would suggest taking a look at this stackoverflow question and answer for how to do that.

Encode and decode Bitmapdata for encryption

Flash CS6 (AS3):
This is what I am trying to accomplish...
Convert bitmapdata -> bytearray -> encode using JPEGencoder (PNGencoder, or any other alternatives) -> base64 string.
Once i get hold of this base64 string, I will be able to use my string obfuscation algorithm, and send that string to php.
Now the reverse (decryption) to get back my original image.
Load the obfuscated string from php into flash -> deobfuscation algorithm -> decodebase64toBytearray -> bitmapdata
The challenge for me rite now is in converting base64 to bytearray, and then to bitmapdata.
Can someone guide me on how to get back the bitmap image from base64 string?
var pBytes:ByteArray = Base64.decodeToByteArray(pString);
var pBitmapData:BitmapData = PNGEncoder2.decode(pBytes);

Uncompressing a Gzip format?

I am facing a problem with Gzip uncompressing.
The situation is like this. I have some text in UTF-8 format. Now this text is compressed using gzdeflate() function in PHP and then stored in a blob object in Mysql.
Now I tried to retrieve the blob object and then used Java's Gzip Stream to un compress it. But it throws an error saying that it is not in GZIP format.
I even used Inflater in Java to do the same but now I get "DataFormatException:incorrect header check". The code for the inflater is as below.
//rs is the resultset
//blobAsBytes is the byte array
while(rs.next()){
blob = rs.getBlob("old_text");
int blobLength = (int) blob.length();
blobAsBytes = blob.getBytes(1, blobLength);
}
Inflater decompresser = new Inflater();
decompresser.setInput(blobAsBytes);
byte[] result = new byte[100];
int resultLength = decompresser.inflate(result); // this is the line where the exception is occurring.
decompresser.end();
// Decode the bytes into a String
String outputString = new String(result, 0, resultLength, "UTF-8");
System.out.println(outputString);
I have to do this using Java and get all the text back that is stored in the database.
Can someone please help me with this.
Use gzencode(), not gzdeflate(). The latter does not produce the gzip format, it produces the deflate format. The former does produce the gzip format. The PHP functions are horribly and confusingly named.
Alternatively, use the java.util.zip.Inflater class with nowrap true in the Inflater constructor. That will decode raw deflate data on the Java end.

Silverlight Byte[] to its original file format

is there a way to convert byte[] to its original file format?
Byte[] tempByte = new Byte[content.Length];
tempByte = Convert.FromBase64String(content);
If you have a Base64 encoded string, then yes Convert.FromBase64String will give you back a byte array identical to the one that was converted to a Base64 string.
However, your first line is unnecessary. You are allocating an array equal to the length of content which just gets overwritten by the return value from Convert.FromBase64String.
byte[] tempByte = Convert.FromBase64String(content);
File.WriteAllBytes(path, tempByte);
The byte array should already be having what you originally read from the file. Write the byte array to a file on the disk and you should be good to go!