Daten komprimieren ( zip, deflate )

R

RED-BARON

Fortgeschrittenes Mitglied
19
Hallo Ihrs,
eben erst angemeldet und schon eine Frage :)

Code:
[B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]import[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] java.util.zip.Deflater;[/SIZE]
 
class compress [B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]extends[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] Activity {[/SIZE]
 
[SIZE=2][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]private[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] Deflater [/SIZE][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]mDeflater[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2];[/SIZE]
 
[LEFT][SIZE=2][COLOR=#646464][SIZE=2][COLOR=#646464]@Override[/COLOR][/SIZE][/COLOR][/SIZE]
[B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]public[/COLOR][/SIZE][/COLOR][/SIZE][/B][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]void[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] onCreate(Bundle savedInstanceState) {[/SIZE][/LEFT]
 
[LEFT][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]super[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2].onCreate(savedInstanceState);[/SIZE][/LEFT]
 
[LEFT][SIZE=2]setContentView(R.layout.[/SIZE][I][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]main[/COLOR][/SIZE][/COLOR][/SIZE][/I][SIZE=2]);[/SIZE][/LEFT]
 
[LEFT][SIZE=2]String in = [/SIZE][SIZE=2][COLOR=#2a00ff][SIZE=2][COLOR=#2a00ff]"Hallo, bitte komprimieren"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2];[/SIZE]
[B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]byte[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2][] out= [/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]new[/COLOR][/SIZE][/COLOR][/SIZE][/B][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]byte[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2][1024];[/SIZE][/LEFT]
 
[LEFT][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]try[/COLOR][/SIZE][/COLOR][/SIZE][/B]
[SIZE=2]{[/SIZE]
[SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]mDeflater[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] = [/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]new[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] Deflater([/SIZE][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]mDeflater[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2].[/SIZE][I][U][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]DEFAULT_COMPRESSION[/COLOR][/SIZE][/COLOR][/SIZE][/U][/I][SIZE=2]);[/SIZE][/LEFT]
[SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]// neu[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000c0]mDeflater[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2].setStrategy([/SIZE][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]mDeflater[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2].[/SIZE][I][U][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]DEFAULT_STRATEGY[/COLOR][/SIZE][/COLOR][/SIZE][/U][/I][SIZE=2]);[/SIZE]
[LEFT][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]mDeflater[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2].setInput(in.getBytes());[/SIZE]
[B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]int[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] [U]rc[/U] = [/SIZE][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]mDeflater[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2].deflate(out);[/SIZE][/LEFT]
[SIZE=2]rc = [/SIZE][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]mDeflater[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2].getTotalIn(); // hinzugefügt[/SIZE]
[LEFT][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]// geändert[/COLOR][/SIZE][/COLOR][/SIZE]
[SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]mDeflater[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2].end();[/SIZE]
[SIZE=2]}[/SIZE]
[B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]catch[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2](Exception e)[/SIZE]
[SIZE=2]{[/SIZE]
[SIZE=2]} [/SIZE][/LEFT]
 
 
[SIZE=2]}[/SIZE]
 
[/SIZE]

rc wird der Wert: 2 zugewiesen, deflate() liefert die Anzahl Zeichen im
Ausgabepuffer zurück. 2 Zeichen stehen auch im Puffer, nur kann es sich
dabei nicht um die komprimierten Daten handeln.

edit:
die Prüfung der eingelesenen Zeichen ergibt rc = 24, stimmt also.


Der Ausgabepuffer sollte auf jeden Fall hinreichend groß sein. Wo steckt
der Fehler ?

Vielen Dank im Voraus.

Grüße, RB
 
Zuletzt bearbeitet:
Hallo Shini, danke für die Beispiele :)

Edit:
alles zurück, funktioniert jetzt !

Viele Grüße, RB
 
Zuletzt bearbeitet:
Hi.

Wie hast Du es denn letztlich gelöst? Deflater, DeflaterOutputStream?

Kannst ja mal den funktionierenden Code posten.

Gruß,
Shini
 
Im Grunde habe ich nur das Beispiel umgesetzt:

Code:
[B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]import[/COLOR][/SIZE][/COLOR][/SIZE][/B]
[LEFT][SIZE=2]java.util.zip.Inflater;[/SIZE][/LEFT]
 
[LEFT][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]import[/COLOR][/SIZE][/COLOR][/SIZE][/B][/LEFT]
 
 
[LEFT][SIZE=2]java.util.zip.Deflater;[/SIZE][/LEFT]
 
 
[LEFT][SIZE=2][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]public[/COLOR][/SIZE][/COLOR][/SIZE][/B][/SIZE][/LEFT]
 
 
[LEFT][SIZE=2][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]class[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] test [/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]extends[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] Activity {[/SIZE][/SIZE][SIZE=2][/LEFT]
 
[LEFT][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]private[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] Deflater [/SIZE][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]mDeflater[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2];[/SIZE][/LEFT]
 
[LEFT][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]private[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] Inflater [/SIZE][U][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]mInflater[/COLOR][/SIZE][/COLOR][/SIZE][/U][SIZE=2];[/SIZE][/LEFT]
 
 
 
 
[/SIZE]
 
[LEFT][SIZE=2]String [U]text_out[/U]=[/SIZE][/LEFT]
 
[LEFT][SIZE=2][COLOR=#2a00ff][SIZE=2][COLOR=#2a00ff]""[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2], text_in = [/SIZE][SIZE=2][COLOR=#2a00ff][SIZE=2][COLOR=#2a00ff]"uncompressed data"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2];[/SIZE][/LEFT]
 
 
 
 
[LEFT][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]byte[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2][] in = [/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]new[/COLOR][/SIZE][/COLOR][/SIZE][/B][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]byte[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2][1024];[/SIZE][/LEFT]
 
 
[LEFT][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]byte[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2][] out= [/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]new[/COLOR][/SIZE][/COLOR][/SIZE][/B][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]byte[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2][1024];[/SIZE][/LEFT]
 
 
[LEFT][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]int[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] mResult = 0;[/SIZE][/LEFT]
 
 
 
 
 
 
 
 
 
 
 
 
 
[LEFT][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]try[/COLOR][/SIZE][/COLOR][/SIZE][/B][/LEFT]
 
 
[LEFT][SIZE=2]{[/SIZE][/LEFT]
 
 
[LEFT][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]mDeflater[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] = [/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]new[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] Deflater();[/SIZE][/LEFT]
 

[LEFT][SIZE=2]in = text_in.getBytes();[/SIZE]

[LEFT][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]mDeflater[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2].setInput(in, 0, in.[/SIZE][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]length[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]);[/SIZE]
[LEFT][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]while[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2]( ![/SIZE][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]mDeflater[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2].needsInput() ) {[/SIZE]
[SIZE=2]mResult += [/SIZE][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]mDeflater[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2].deflate(out, mResult, out.[/SIZE][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]length[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]-mResult);[/SIZE]
[SIZE=2]}[/SIZE]
[SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]mDeflater[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2].finish();[/SIZE]
[B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]while[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2]( ![/SIZE][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]mDeflater[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2].finished() ) {[/SIZE]
[SIZE=2]mResult += [/SIZE][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]mDeflater[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2].deflate(out, mResult, out.[/SIZE][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]length[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]-mResult);[/SIZE]
[SIZE=2]} [/SIZE]
[SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]mDeflater[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2].reset();[/SIZE]
[SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]mDeflater[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] = [/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]null[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2];[/SIZE][/LEFT]
[/LEFT]


[/LEFT]

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
[LEFT][SIZE=2][COLOR=#3f7f5f][SIZE=2][COLOR=#3f7f5f]// [U]Decompress[/U] the bytes[/COLOR][/SIZE][/COLOR][/SIZE][/LEFT]
 
 
[LEFT][SIZE=2]Inflater decompresser = [/SIZE][/LEFT]
 
 
 
 
 
 
 
 
[LEFT][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]new[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] Inflater();[/SIZE][/LEFT]
 
 
[LEFT][SIZE=2]decompresser.setInput(out, 0, mResult);[/SIZE][/LEFT]
 
 
[LEFT][SIZE=2]mResult = decompresser.inflate(in);[/SIZE][/LEFT]
 

[LEFT][SIZE=2]decompresser.end();[/SIZE]
 
[LEFT][SIZE=2]text_out = [/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]new[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] String(in);[/SIZE][/LEFT]
[/LEFT]

 
 
 
 
 }
 
 [B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]catch[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2](Exception e)[/SIZE]
 
 
[LEFT][SIZE=2]{[/SIZE][/LEFT]
 

[LEFT][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]int[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] [U]rc[/U] = 0;[/SIZE][/LEFT]

 
 
 
[LEFT][SIZE=2]} [/SIZE][/LEFT]
 
[LEFT]}[/LEFT]



Das Inflater-Beispiel habe ich hier gefunden




denke aber das kann man auch analog zum Deflater-Bsp. mit ein​


paar Schleifen lösen oder das Deflater-Bsp. von der Sun Seite nehmen :)


Irgendwie habe ich einfach nix gefunden mit google gestern, weiß nicht​


was los war :eek:


Als nächstes steht jetzt das umschalten eines "normalen" Sockets zu


einem SSL-Socket auf dem Plan um aus dem ganzen Mix irgendwann mal


das MySQL Protokoll zu implementieren mit Kompression und Verschlüsselung.​

Unverschlüsselt, funktioniert die Anmeldung am Server schon, auch
Abfragen können gesendet werden. Die Ergebnissauswertung ist aber
noch TODO :D und ... es werden nur die alten kurzen Passwörter
unterstützt, die neuen langen 40 Zeichen breiten Passwörter werden
mit SH1 oder so verwurstet. Auf dem MDA mit openssl hab ich das hin-
bekommen aber mit dem Android-SDK kenn ich mich noch nicht so aus.​

Daher nur die von Hand verwursteten kurzen Passwörter.​

Wie gesagt, Tag 5 der Android Challenge :cool: und eigentlich Java newbie.
Die API-Beispiele habe ich schon angesehen und <versucht> zu verstehen
was die Abläufe betrifft, naja ...​

Gegenüber Black-Berry bietet aber die Java-Engine offensichtlich viel mehr,
was schon mal positiv ist :)


Mühsam ernährt sich das Eichhörnchen, in diesem Sinn - viele Grüße, RB​
 

Ähnliche Themen

A
Antworten
10
Aufrufe
1.018
swa00
swa00
A
Antworten
10
Aufrufe
1.903
swa00
swa00
B
Antworten
7
Aufrufe
1.342
bqde
B
Zurück
Oben Unten