|
|||||||
|
|
|
![]() |
|
|
Strumenti |
|
|
#1 | |
|
Senior Member
Iscritto dal: Sep 2001
Città: de_legato
Messaggi: 792
|
[JAVA]Compressione dati, calssi Deflater/Inflater
Vi posto il codice che penso sia molto più esplicativo:
Codice:
import java.io.ByteArrayOutputStream;
import java.util.zip.Deflater;
import java.util.zip.Inflater;
public class Compression {
private byte[] buf = null;
private Deflater compressor = null;
private Inflater decompressor = null;
private ByteArrayOutputStream baos = null;
public Compression() {
buf = new byte[1024];
compressor = new Deflater(Deflater.BEST_COMPRESSION,true);
decompressor = new Inflater(true);
}
public synchronized byte[] Compress(byte[] a) throws Exception {
return Compress(a, null);
}
public synchronized byte[] Compress(byte[] a, byte[] dictionary) throws Exception {
compressor.reset();
if(dictionary!=null)
compressor.setDictionary(dictionary); <---
compressor.setInput(a);
compressor.finish();
baos = new ByteArrayOutputStream(a.length);
while (!compressor.finished()) {
int count = compressor.deflate(buf);
baos.write(buf,0,count);
}
baos.close();
return baos.toByteArray();
}
public synchronized byte[] DeCompress(byte[] a) throws Exception {
return DeCompress(a, null);
}
public synchronized byte[] DeCompress(byte[] a, byte[] dictionary) throws Exception {
decompressor.reset();
if(decompressor.needsDictionary())
decompressor.setDictionary(dictionary);
decompressor.setInput(a);
baos = new ByteArrayOutputStream(a.length);
while (!decompressor.finished()) {
int count = decompressor.inflate(buf);
baos.write(buf,0,count);
}
baos.close();
return baos.toByteArray();
}
}
Quote:
__________________
---------------------------------------------- File reality.sys corrupted, Reboot Universe? Y/N ---------------------------------------------- |
|
|
|
|
|
|
#2 |
|
Senior Member
Iscritto dal: Sep 2001
Città: de_legato
Messaggi: 792
|
[JAVA]formattazione codice html
risolto ....evidentemente l'opzione nowrap che passo ai due costruttori non va daccordo con l'utilizzo del dizionario
__________________
---------------------------------------------- File reality.sys corrupted, Reboot Universe? Y/N ---------------------------------------------- |
|
|
|
|
|
#3 |
|
Senior Member
Iscritto dal: Sep 2001
Città: de_legato
Messaggi: 792
|
Questa è la classe finale:
Codice:
package odbase;
import java.io.ByteArrayOutputStream;
import java.util.zip.Deflater;
import java.util.zip.Inflater;
public class Compression {
private byte[] buf = null;
private byte[] dictionary = null;
private Deflater compressor = null;
private Inflater decompressor = null;
private ByteArrayOutputStream baos = null;
public Compression() {
buf = new byte[1024];
compressor = new Deflater(Deflater.BEST_COMPRESSION,true);
decompressor = new Inflater(true);
}
public Compression(byte[] dictionary) {
buf = new byte[1024];
this.dictionary = dictionary;
compressor = new Deflater(Deflater.BEST_COMPRESSION,false);
decompressor = new Inflater(false);
}
public boolean useDictionary() {
return dictionary!=null;
}
public synchronized byte[] Compress(byte[] a) throws Exception {
compressor.reset();
if(useDictionary())
compressor.setDictionary(dictionary);
compressor.setInput(a);
compressor.finish();
baos = new ByteArrayOutputStream(a.length);
while (!compressor.finished()) {
int count = compressor.deflate(buf);
baos.write(buf,0,count);
}
baos.close();
return baos.toByteArray();
}
public synchronized byte[] DeCompress(byte[] a) throws Exception {
decompressor.reset();
decompressor.setInput(a);
baos = new ByteArrayOutputStream(a.length);
while (!decompressor.finished()) {
int count = decompressor.inflate(buf);
if(decompressor.needsDictionary())
decompressor.setDictionary(dictionary);
baos.write(buf,0,count);
}
baos.close();
return baos.toByteArray();
}
}
Codice:
String dic = "abcdefghyijlmnopqrstuwvz"; Compression c = new Compression(dic.getBytes()); String prova = "wdhgwehwyhbswqyedbwtefdvwteyfdvwtyefcyjetwcfwyefyerfcuywrefuywtrfytwerfvytwerdyfvwerdyuvwterfvdytrehcdfvgcngjaeqwhieywytgbcbwewqiydyugyvketkwcdbkundqeiwrnuweigbcufeehlfhenjfewòfncifuewbfucehrufbvregfyvuberftcyelfucynheòlwruwermefjlrfnvjghbrlweuyòweounhfuwevrbgrelufybwehluefbf"; System.out.println(prova.length()); byte[] aux = c.Compress(prova.getBytes()); System.out.println(aux.length); aux = c.DeCompress(aux); System.out.println(aux.length);
__________________
---------------------------------------------- File reality.sys corrupted, Reboot Universe? Y/N ---------------------------------------------- |
|
|
|
|
| Strumenti | |
|
|
Tutti gli orari sono GMT +1. Ora sono le: 06:02.


















