Вы находитесь на странице: 1из 2

import java.io.ByteArrayOutputStream; java.io.IOException; import java.util.zip.GZIPOutputStream; import java.util.zip.

*; public class zipUtil{ public static String compress(String str){ if (str == null || str.length() == 0) { return str; } ByteArrayOutputStream out = new ByteArrayOutputStream(); GZIPOutputStream gzip = new GZIPOutputStream(out); gzip.write(str.getBytes()); gzip.close(); return out.toString("ISO-8859-1"); } public static void main(String[] args) throws IOException { String string = "admin"; System.out.println("after compress:"); System.out.println(ZipUtil.compress(string)); } } /******************************************************************************* *********************/ 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; }

Вам также может понравиться