Ionic.zip Gzipstream Decompress ,Bad state (invalid stored block lengths) - gzipstream

i use ionic.zip gzipstream compress and decompress byte[],
it can compress well,but when it decompressed from byte[] compressed it told
Bad state (invalid stored block lengths)
my codes bellow
Trace.WriteLine(s.Length);
var b = Encoding.UTF8.GetBytes(s);
Trace.WriteLine(b.Length);
byte[] b2;
var sw = new Stopwatch();
sw.Start();
using (var m = new MemoryStream())
{
var stream = new GZipStream(m, CompressionMode.Compress,true);
stream.Write(b,0,b.Length);
stream.Flush();
b2 = m.GetBuffer();
stream.Close();
sw.Stop();
Trace.WriteLine(sw.ElapsedMilliseconds);
Trace.WriteLine(b2.Length);
}
using (var m2 = new MemoryStream())
{
m2.Write(b2,0,b2.Length);
m2.Position = 0;
var stream = new GZipStream(m2,CompressionMode.Decompress,true);
var m3 = new MemoryStream();
var buffer = new byte[1024];
var n = 1;
while (n != 0)
{
n = stream.Read(buffer, 0, buffer.Length);
if (n > 0)
{
m3.Write(buffer,0,n);
}
}
var b3 = m3.GetBuffer();
Trace.WriteLine(b3);
}
the decompress code is what the document said.here
and i found no other docs ,
what should i do when decompress?

Ah~~~, it's so fool.
there are byte[] compress and decompress methods.
GZipStream.CompressBuffer() and GZipStream.UncompressBuffer()

Related

iText7 how to add another pdf as background to exising pdf

I have 2 PDF files. under is single-page PDF file and must be a background for every page of original...
This is not working
public static byte[] overlay(byte[] original, byte[] under)
{
using (var resultStream = new MemoryStream())
{
var pdfWriter = new PdfWriter(resultStream);
var pdfReader = new PdfReader(new MemoryStream(original));
var pdfDoc = new PdfDocument(pdfReader, pdfWriter);
for (var p = 1; p <= pdfDoc.GetNumberOfPages(); p++)
{
var pdfUnder = new PdfDocument(new PdfReader(new MemoryStream(under)));
var pdfUnderPage = pdfUnder.GetFirstPage().CopyAsFormXObject(pdfDoc);
var page = pdfDoc.GetPage(p);
var canvas = new PdfCanvas(page.NewContentStreamBefore(),
page.GetResources(), pdfDoc);
canvas.AddXObjectAt(pdfUnderPage, 0, 0);
pdfUnder.Close();
}
pdfDoc.Close();
return resultStream.ToArray();
}
}

To Extract .7zip file using SharpCompress library in windows phone but getting exception

IArchive archive = null;
IReader reader = null;
archive = SevenZipArchive.Open(fileStream, Options.LookForHeader);
reader = archive.ExtractAllEntries();
while (reader.MoveToNextEntry())
{
if (!reader.Entry.IsDirectory)
{
Stream _redaer = new MemoryStream();
reader.WriteEntryTo(_redaer);
fileName = reader.Entry.FilePath;
int index = fileName.LastIndexOf("/");
string file = fileName.Substring(index + 1, (fileName.Length - (index + 1)));
using (binaryReader = new BinaryReader(_redaer, encoding))
{
long fileLength = _redaer.Length;
MemoryStream ms = new MemoryStream();
_redaer.Position = 0;
_redaer.CopyTo(ms);
byte[] buteArray = ms.ToArray();
SaveToIsoStore(fileName, buteArray);
}
}
}
This code gives exception of type SharpCompress.Common.InvalidFormatException,Please provide the solution in wp7.

i want to play music from webSite with the Mediaelement ," media.setSource()"

i get the stream from webSite ,then put it in isolatedStorage into IsolatedstorageStream ,
but it don't work ,no error no sound , what's wrong ????
HttpWebResponse reponse = request.EndGetResponse(result) as HttpWebResponse;
if (reponse.StatusCode == HttpStatusCode.OK)
{
Stream stream=reponse.GetResponseStream();
SaveMusic(stream, "music");
ReadMusic("music");
Deployment.Current.Dispatcher.BeginInvoke(
() =>
{
me.AutoPlay = true;
me.Volume = 100;
me.SetSource(songStream);
me.Play();
});
}
ok thanks keyboardP for your help ;here is my code
protected void SaveMusic(Stream stream,string name)
{
IsolatedStorageFile fileStorage = IsolatedStorageFile.GetUserStoreForApplication();
if (!fileStorage.DirectoryExists("Source/Music"))
{
fileStorage.CreateDirectory("Source/Music");
}
using (IsolatedStorageFileStream fileStream = IsolatedStorageFile.GetUserStoreForApplication().OpenFile("Source\\Music\\" + name + ".mp3", FileMode.Create))
{
byte[] bytes = new byte[stream.Length];
stream.Read(bytes, 0, bytes.Length);
fileStream.Write(bytes, 0, bytes.Length);
fileStream.Flush();
}
}
protected void ReadMusic(string name)
{
using (IsolatedStorageFile fileStorage = IsolatedStorageFile.GetUserStoreForApplication())
{
songStream = null;
songStream = new IsolatedStorageFileStream("Source\\Music\\" + name + ".mp3", FileMode.Open, fileStorage);
}
}
Assuming your saving and reading code is correct, your stream's position might be at the end. Try adding
songStream.Position = 0;
before SetSource(songStream);
Try using this to save the file:
using (var fileStorage = IsolatedStorageFile.GetUserStoreForApplication())
{
var buffer = new byte[1024];
using (var myIsStream = fileStorage.OpenFile("Source\\Music\\" + name + ".mp3", FileMode.CreateNew))
{
int bytesRead = 0;
while ((bytesRead = stream.Read(buffer, 0, 1024)) > 0)
myIsStream.Write(buffer, 0, bytesRead);
}
}

Convert image and audio files to binary in Java

I want to convert an image and an audio file into a binary stream, process it and then reconstruct the image from the same binary stream in Java. How can I do that? Has anybody worked on this? Please help me out as soon as possible. Any hints or pseudocode will be highly appreciated.
This is how I tried to do it but it just creates an empty file while reconstructing the image.
For image to binary:-
File file = new File("E:\\image.jpg");
BufferedImage img = ImageIO.read(file);
// write image to byte array in-memory (jpg format)
ByteArrayOutputStream b = new ByteArrayOutputStream();
ImageIO.write(img, "jpg", b);
byte[] jpgByteArray = b.toByteArray();
// convert it to a String with 0s and 1s
StringBuilder sb = new StringBuilder();
for (byte by : jpgByteArray) {
sb.append(Integer.toBinaryString(by & 0xFF));
For binary to image:-
byte[] original = obj.orig_seq.getBytes();
InputStream in = new ByteArrayInputStream(original);
BufferedImage img = ImageIO.read(in);
ImageIO.write(img, "jpg",
new File("E:\\mypic_new.jpg"));
To use Binary String you can use this
StringBuilder sb = new StringBuilder();
try (BufferedInputStream is = new BufferedInputStream(new FileInputStream("YourImage.jpg"))) {
for (int i; (i = is.read()) != -1;) {
String temp = "0000000" + Integer.toBinaryString(i).toUpperCase();
if (temp.length() == 1) {
sb.append('0');
}
temp = temp.substring(temp.length() - 8);
sb.append(temp).append(' ');
}
}
System.out.print(sb);
and here is your desired output
0000000 11111111 00000111 00011111 00101111 01111111
you can also use Hexadecimal and convert your file into a simple string like this (suggestion)
StringBuilder sb = new StringBuilder();
try (BufferedInputStream is = new BufferedInputStream(new FileInputStream("YourFile.txt"))) {
for (int i; (i = is.read()) != -1;) {
String temp = Integer.toHexString(i).toUpperCase();
if (temp.length() == 1) {
sb.append('0');
}
sb.append(temp).append(' ');
}
}
System.out.print(sb);
and the output would be something like this
00 FF 07 1F 2F 7F

Blackberry - get image data from bitmap

how to get image data from a bitmap image ? i searched, but i cant find a solution
int height=bmp.getHeight();
int width=bmp.getWidth();
int[] rgbdata = new int[width*height];
ByteArrayOutputStream bos = new ByteArrayOutputStream();
DataOutputStream dos = new DataOutputStream(bos);
//Graphics g = new Graphics(bmp);
bmp.getARGB(rgbdata,0,width,0,0,width,height);
for (int i = 0; i < rgbdata.length ; i++) {
if (rgbdata[i] != -1)
{
dos.writeInt(rgbdata[i]);
dos.flush();
}
}
bos.flush();
Try this:
PNGEncoder encoder = new PNGEncoder(bitmap, true);
byte[] imageBytes = encoder.encode(true);
And to get EncodedImage from byte array:
EncodedImage fullImage = EncodedImage.createEncodedImage(imageBytes, 0, imageBytes.length);

Resources