壓縮解壓縮工具 ZipInputStream ZipOutputStream

 // 壓縮

 public static void makeZip(byte[] bytes) {
  ByteArrayOutputStream bos = new ByteArrayOutputStream();
  ZipOutputStream zout = new ZipOutputStream(bos);
  try {
   zout.write(bytes);
  } catch (IOException e) {
   logger.error(e);
  } finally {
   try {
    bos.close();
    zout.close();
   } catch (IOException e) {
    logger.error(e);
   }

  }
 }

 public static void getZip(byte[] bytes) {
  ByteArrayInputStream in = new ByteArrayInputStream(bytes);
  ZipInputStream zin = new ZipInputStream(in);
  ByteArrayOutputStream out = new ByteArrayOutputStream();
  try {
   zin.getNextEntry();
  } catch (IOException e) {
   logger.error(e);
  }
  byte[] buffer = new byte[bytes.length];
  int offset = -1;
  try {
   while ((offset = zin.read(buffer)) != -1) {
    out.write(buffer, 0, offset);
   }
  } catch (IOException e) {

   logger.error(e);
  } finally {
   try {
    in.close();
    zin.close();
    out.close();
   } catch (IOException e) {
    logger.error(e);
   }

  }
 }

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章