13
public static String compressString(String str) throws IOException{
    if (str == null || str.length() == 0) {
        return str;
    }
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    GZIPOutputStream gzip = new GZIPOutputStream(out);
    gzip.write(str.getBytes());
    gzip.close();
    Gdx.files.local("gziptest.gzip").writeString(out.toString(), false);
    return out.toString();
}

When I save that string to a file, and run gunzip -d file.txt in unix, it complains:

gzip: gzip.gz: not in gzip format
kelorek
  • 6,042
  • 6
  • 29
  • 32
  • Why don't you simply use a [FileOutputStream (in place of the ByteArrayOutputStream)](http://stackoverflow.com/questions/5994674/java-save-string-as-gzip-file)? Have you tried what happens then? – Sven Amann Sep 27 '13 at 07:04
  • It's libgdx, which is a cross-platform game development library. I only wrote it to a file to troubleshooting. I've actually been trying to send the string via http POST request to my flask server, but the server side complained that the string isn't valid gzip. – kelorek Sep 27 '13 at 07:09
  • I guess that the problem is your conversion of the compressed data to a string. I think you should treat the result as a byte[]. Can libgdx write a byte[] to a file? – Sven Amann Sep 27 '13 at 07:12
  • It can. Try Gdx.files.local("gziptest.gzip").writeBytes(out.getBytes(), false). What happens? – Sven Amann Sep 27 '13 at 07:14

3 Answers3

15

Try to use BufferedWriter

public static String compressString(String str) throws IOException{
if (str == null || str.length() == 0) {
    return str;
}

BufferedWriter writer = null;

try{
    File file =  new File("your.gzip")
    GZIPOutputStream zip = new GZIPOutputStream(new FileOutputStream(file));

    writer = new BufferedWriter(new OutputStreamWriter(zip, "UTF-8"));

    writer.append(str);
}
finally{           
    if(writer != null){
     writer.close();
     }
  }
 }

About your code example try:

public static String compressString(String str) throws IOException{
if (str == null || str.length() == 0) {
    return str;
}
ByteArrayOutputStream out = new ByteArrayOutputStream(str.length());
GZIPOutputStream gzip = new GZIPOutputStream(out);
gzip.write(str.getBytes());
gzip.close();

byte[] compressedBytes = out.toByteArray(); 

Gdx.files.local("gziptest.gzip").writeBytes(compressedBytes, false);
out.close();

return out.toString(); // I would return compressedBytes instead String
}
Maxim Shoustin
  • 77,483
  • 27
  • 203
  • 225
  • That makes a valid gzip object. I'm really looking to return a string though. Can I bypass writing the file? – kelorek Sep 27 '13 at 07:15
  • For your example try first: `ByteArrayOutputStream out = new ByteArrayOutputStream(str.length());` – Maxim Shoustin Sep 27 '13 at 07:24
  • Take a look on my edit I posted above, I changed your code a bit – Maxim Shoustin Sep 27 '13 at 07:28
  • That works. At some point I have to convert it to string to send up to my flask application-- why do you recommend returning as a `byte[]`? – kelorek Sep 27 '13 at 07:39
  • because its unreadable String I think, IDK what you want to do with that after. If you send `byte[]` you can after decompress it back – Maxim Shoustin Sep 27 '13 at 07:42
  • Ok that clears up my confusion. (Thank you.) I'm sending touch screen log data from my game up to my server so I can do analytics on it later. I'm going to base64 encode the byte[] for sending up. I think that will work if I understand things correctly. – kelorek Sep 27 '13 at 07:47
  • @Downvoter please give description why down-vote. thanks – Maxim Shoustin Apr 15 '14 at 15:17
4

Try that :

//...

String string = "string";

FileOutputStream fos = new FileOutputStream("filename.zip");

GZIPOutputStream gzos = new GZIPOutputStream(fos);
gzos.write(string.getBytes());
gzos.finish();

//...
0

Save bytes from out with FileOutputStream

FileOutputStream fos = new FileOutputStream("gziptest.gz");
fos.write(out.toByteArray());
fos.close();

out.toString() seems suspicious, the result will be unreadable, if you dont care then why not to return byte[], if you do care it would look better as hex or base64 string.

Evgeniy Dorofeev
  • 133,369
  • 30
  • 199
  • 275