2013-09-27 13 views
9
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(); 
} 

Quando li risparmio che stringa in un file, ed eseguire gunzip -d file.txt in UNIX, si lamenta:Compress una stringa a gzip in Java

gzip: gzip.gz: not in gzip format 
+0

Perché non si utilizza semplicemente un [FileOutputStream (al posto di ByteArrayOutputStream)] (http://stackoverflow.com/questions/5994674/java-save-string-as-gzip-file)? Hai provato cosa succede allora? –

+0

È libgdx, una libreria di sviluppo di giochi multipiattaforma. L'ho solo scritto su un file per la risoluzione dei problemi. In realtà sto provando a inviare la stringa tramite la richiesta HTTP POST al mio server flask, ma il lato server ha lamentato che la stringa non è valida gzip. – kelorek

+0

Immagino che il problema sia la conversione dei dati compressi in una stringa. Penso che dovresti considerare il risultato come un byte []. Può libgdx scrivere un byte [] in un file? –

risposta

11

Provare a usare 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(); 
    } 
    } 
} 

Chi il tuo esempio di codice prova:

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 
} 
+0

Ciò rende un oggetto gzip valido. Sto davvero cercando di restituire una stringa però. Posso ignorare la scrittura del file? – kelorek

+0

Per il tuo esempio prova prima: 'ByteArrayOutputStream out = new ByteArrayOutputStream (str.length());' –

+0

Che non funziona. – kelorek

2

Prova che:

//... 

String string = "string"; 

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

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

//... 
0

Salva byte da fuori con FileOutputStream

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

out.toString() sembra sospetto, il risultato sarà illeggibile, se non la cura allora perché non tornare byte [ ], se si cura che sarebbe meglio come stringa hex o base64.

+0

d'accordo, vorrei restituire 'byte []' da 'out.toByteArray()' –