读写zip格式的文件

1.结果集能得到byte[]类型的数据

2.String可以设置编码格式

3.读写完毕都要关闭流input、output

public Object nullSafeGet(ResultSet rs, String[] names, Object entity) throws HibernateException, SQLException {
  String result = null;
  byte[] bs = rs.getBytes(names[0]);
  if(bs == null)
   return result;
  //解压字节流为字符串
  ZipInputStream zipins = new ZipInputStream(new ByteArrayInputStream(bs));
  ByteArrayOutputStream out = new ByteArrayOutputStream();
  byte[] buf = new byte[1024];
  int read = -1;
  try {
   zipins.getNextEntry();
   while((read=zipins.read(buf))>0){
    out.write(buf, 0, read);
   }
   result = new String(out.toByteArray(), "GBK");
   out.close();
   zipins.close();
  } catch (Exception e) {
   e.printStackTrace();
  }
  return result;
 }

 public void nullSafeSet(PreparedStatement ps, Object value, int index) throws HibernateException, SQLException {
  String values = (String)value;
  if(values == null){
   ps.setObject(index, null);
   return;
  }
  //把字符串压缩为字节流储存
  ByteArrayOutputStream out = new ByteArrayOutputStream();
  ZipOutputStream zipout = new ZipOutputStream(out);
  try {
   zipout.putNextEntry(new ZipEntry("s"));
   zipout.write(values.getBytes("GBK"));
   zipout.close();
   out.close();
   ps.setObject(index, out.toByteArray());
  } catch (Exception e) {
   e.printStackTrace();
  }
 }

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值