• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

LZW Compression / Decompression

 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!

 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic