Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

LZW Compression / Decompression  RSS feed

 
Kevin Behr
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey all-

Can you guys offer me some help? I am trying to code up a program that will accept a large file, compress it, and then be able to decompress it. Here is what I have, but I don't think it's working out

Thanks!

 
Paul Clapham
Sheriff
Posts: 22493
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I can't tell what your program does, and you didn't say what it's supposed to do either. At least in any detail. But I do notice that you're converting bytes to chars a lot, and then converting them back to bytes by writing them to an output stream.

Your comments say something about "ASCII characters" but Java doesn't limit itself to ASCII. Java characters are two-byte things that represent UNICODE characters, and converting a character to a byte doesn't just drop the leading byte. Instead it does a conversion that assumes the character is text, which is likely to be an incorrect assumption for compressed data. If a char isn't represented in your system's default character set, then converting it to bytes results in '?'.

So: don't use chars, use bytes. I know that will require some changes because you can't just use String concatenation to collect bytes together, but chars are wrong here.
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16028
87
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't know if you want to implement your own LZW compression for some reason, but did you know that Java already includes classes for reading and writing ZIP files? See the API documentation of the package java.util.zip.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!