S
sven abels
Hi!
I need a method to compress a (quite large) String and to uncompress it
later.
I wrote the following code that uses the ZIP-Functions of Java.
However: This functions (I think it's the compress-function) don't work
properly on some strings.
I get an error message when uncompressing the string:
java.util.zip.ZipException: incomplete distance tree or sometimes:
java.util.zip.ZipException: invalid entry size
Can somebody give me a tip?
If somebody has a faster / other method to simply compress and uncompress a
String (it can be a quite bad compression, too), then this would be
perfectly fine, too...
Here'S my code which has the described bug:
------------
public static String compress(String stream)
{
ByteArrayInputStream fis=null;
ByteArrayOutputStream fos=null;
String erg="";
try {
fis = new ByteArrayInputStream(stream.getBytes());
fos = new ByteArrayOutputStream();
ZipOutputStream zos =
new ZipOutputStream(fos);
ZipEntry ze = new ZipEntry("name1");
zos.putNextEntry(ze);
final int BUFSIZ = 4096;
byte inbuf[] = new byte[BUFSIZ];
int n;
while ((n = fis.read(inbuf)) != -1)
zos.write(inbuf, 0, n);
fis.close();
fis = null;
zos.close();
}
catch (IOException e) {
System.err.println(e);
e.printStackTrace();
}
finally {
try {
if (fis != null)
fis.close();
if (fos != null)
{
fos.close();
erg=new String(fos.toByteArray());
}
}
catch (IOException e) {
e.printStackTrace();
}
}
return erg;
}
public static String uncompress(String stream)
{
ByteArrayInputStream fis=null;
ByteArrayOutputStream fos=null;
String erg="";
try {
fis = new ByteArrayInputStream(stream.getBytes());
fos = new ByteArrayOutputStream();
ZipInputStream zis = new ZipInputStream(fis);
ZipEntry ze = zis.getNextEntry();
final int BUFSIZ = 4096;
byte inbuf[] = new byte[BUFSIZ];
int n;
while ((n = zis.read(inbuf, 0, BUFSIZ)) != -1)
{
System.out.println(".");
fos.write(inbuf, 0, n);
}
zis.close();
fis = null;
fos.close();
}
catch (IOException e) {
System.err.println(e);
e.printStackTrace();
}
finally {
try {
if (fis != null)
fis.close();
if (fos != null)
fos.close();
erg=new String(fos.toByteArray());
}
catch (IOException e) {
e.printStackTrace();
}
}
return erg;
}
-------------
I need a method to compress a (quite large) String and to uncompress it
later.
I wrote the following code that uses the ZIP-Functions of Java.
However: This functions (I think it's the compress-function) don't work
properly on some strings.
I get an error message when uncompressing the string:
java.util.zip.ZipException: incomplete distance tree or sometimes:
java.util.zip.ZipException: invalid entry size
Can somebody give me a tip?
If somebody has a faster / other method to simply compress and uncompress a
String (it can be a quite bad compression, too), then this would be
perfectly fine, too...
Here'S my code which has the described bug:
------------
public static String compress(String stream)
{
ByteArrayInputStream fis=null;
ByteArrayOutputStream fos=null;
String erg="";
try {
fis = new ByteArrayInputStream(stream.getBytes());
fos = new ByteArrayOutputStream();
ZipOutputStream zos =
new ZipOutputStream(fos);
ZipEntry ze = new ZipEntry("name1");
zos.putNextEntry(ze);
final int BUFSIZ = 4096;
byte inbuf[] = new byte[BUFSIZ];
int n;
while ((n = fis.read(inbuf)) != -1)
zos.write(inbuf, 0, n);
fis.close();
fis = null;
zos.close();
}
catch (IOException e) {
System.err.println(e);
e.printStackTrace();
}
finally {
try {
if (fis != null)
fis.close();
if (fos != null)
{
fos.close();
erg=new String(fos.toByteArray());
}
}
catch (IOException e) {
e.printStackTrace();
}
}
return erg;
}
public static String uncompress(String stream)
{
ByteArrayInputStream fis=null;
ByteArrayOutputStream fos=null;
String erg="";
try {
fis = new ByteArrayInputStream(stream.getBytes());
fos = new ByteArrayOutputStream();
ZipInputStream zis = new ZipInputStream(fis);
ZipEntry ze = zis.getNextEntry();
final int BUFSIZ = 4096;
byte inbuf[] = new byte[BUFSIZ];
int n;
while ((n = zis.read(inbuf, 0, BUFSIZ)) != -1)
{
System.out.println(".");
fos.write(inbuf, 0, n);
}
zis.close();
fis = null;
fos.close();
}
catch (IOException e) {
System.err.println(e);
e.printStackTrace();
}
finally {
try {
if (fis != null)
fis.close();
if (fos != null)
fos.close();
erg=new String(fos.toByteArray());
}
catch (IOException e) {
e.printStackTrace();
}
}
return erg;
}
-------------