写入内容到文件

public static void writeBytesToFile() throws IOException{
String s = "aaaaaaaaD等等";
byte[] bs= s.getBytes();
OutputStream out = new FileOutputStream("d:/abc.txt");
InputStream is = new ByteArrayInputStream(bs);
byte[] buff = new byte[1024];
int len = 0;
while((len=is.read(buff))!=-1){
out.write(buff, 0, len);
}
is.close();
out.close();
}

gzip压缩byte[]

byte[] b = null;
ByteArrayInputStream bis = new ByteArrayInputStream(byteIn);
GZIPInputStream gzip = new GZIPInputStream(bis);
byte[] buf = new byte[1024];
int num = -1;
ByteArrayOutputStream baos = new ByteArrayOutputStream();
while ((num = gzip.read(buf, 0, buf.length)) != -1) {
baos.write(buf, 0, num);
}
b = baos.toByteArray();
baos.flush();
baos.close();
gzip.close();
bis.close();

zip压缩byte[]

byte[] b = null;
ByteArrayInputStream bis = new ByteArrayInputStream(byteIn);
ZipInputStream zip = new ZipInputStream(bis);
ZipEntry nextEntry = zip.getNextEntry();
while (zip.getNextEntry() != null) {
byte[] buf = new byte[1024];
int num = -1;
ByteArrayOutputStream baos = new ByteArrayOutputStream();
while ((num = zip.read(buf, 0, buf.length)) != -1) {
baos.write(buf, 0, num);
}
b = baos.toByteArray();
baos.flush();
baos.close();
}
zip.close();
bis.close();

根据byte数组,生成txt文件

package com.hou.test1;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Date; public class Test4 {
public static void main(String[] args) {
byte[] b = "123abvc到达".getBytes();
String filePath="d:";
String fileName=new Date().getTime()+".txt";
getFile(b,filePath,fileName);
System.out.println("压缩成功");
} /**
* 根据byte数组,生成文件
*/
public static void getFile(byte[] bfile, String filePath,String fileName) {
BufferedOutputStream bos = null;
FileOutputStream fos = null;
File file = null;
try {
File dir = new File(filePath);
if(!dir.exists()&&dir.isDirectory()){//判断文件目录是否存在
dir.mkdirs();
}
file = new File(filePath+"\\"+fileName);
fos = new FileOutputStream(file);
bos = new BufferedOutputStream(fos);
bos.write(bfile);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (bos != null) {
try {
bos.close();
} catch (IOException e1) {
e1.printStackTrace();
}
}
if (fos != null) {
try {
fos.close();
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
}
}

根据byte数组,生成zip文件

public static void main(String[] args) {
byte[] b = "123abvc到达".getBytes();
getFile1(b);
System.out.println("压缩成功");
} /**
* 根据byte数组,生成文件
*/
public static void getFile1(byte[] byteIn){
try {
File zipFile=new File("d:/COMPLETE"+new Date().getTime()+".zip");
FileOutputStream zipOut;
//以上是将创造一个zip格式的文件
zipOut = new FileOutputStream(zipFile);
ZipOutputStream zip=new ZipOutputStream(zipOut);
ZipEntry zipEntry1=new ZipEntry(new Date().getTime()+"");
zip.putNextEntry(zipEntry1);
byte [] byte_s="测试内容aaa".getBytes();
// zip.write(byte_s,0,byte_s.length);
zip.write(byteIn,0,byteIn.length);
zip.close();
zipOut.close();
} catch (Exception e) {
// TODO: handle exception
}
}

HttpGet 获取字节数组压缩成zip,.tar.gz文件

HttpGet httpGet = new HttpGet(url);
httpGet.addHeader("authorization", head);
httpGet.addHeader("Transfer-Encoding", "GZIP"); try {
HttpResponse response = HttpClients.createDefault().execute(httpGet);
byte[] byteIn = EntityUtils.toByteArray(response.getEntity()); CommonUtils.getFileFromBytes(byteIn, "QUNAR_ONE_COMMON_PRYPAY_"+System.currentTimeMillis() , ".tar.gz");
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} /**
* 二进制流转换成文件
*
* @param byteArray
* 请求二进制流数据
* @param prefix
* 文件名前缀
* @param suffix
* 文件名后缀
* @return zip压缩文件
*/
public static File getFileFromBytes(byte[] byteArray, String prefix,String suffix) {
BufferedOutputStream stream = null;
File file = null;
String str="";
try {
file = new File(FILE_PATH+prefix+suffix);
file.createNewFile();// 创建临时文件
FileOutputStream fstream = new FileOutputStream(file);
stream = new BufferedOutputStream(fstream);
stream.write(byteArray);
} catch (Exception e) {
logger.error("创建临时文件失败!"+e);
} finally {
if (stream != null) {
try {
stream.close();
} catch (IOException e1) {
logger.error(e1);
}
}
} logger.info("创建临时文件"+file.getPath()+" "+str);
return file;
}

byte字节数组的压缩的更多相关文章

  1. spring boot 使用WebSocket与前端进行byte字节数组交互

    一.装逼前先热热身 无论是比较传统的 web项目 还是近几年流行的前后端分离,后端只独立提供数据交互接口服务的项目,都避免不了数据之间交互格式的选择. 从很早之前的 xml 格式 到现在最火热的jso ...

  2. (八)二进制文件在webservice中的处理(以byte[]字节数组方式)

    一.介绍 二进制文件在webservice中的处理. A:通过byte[]字节数组的方式来传递.比较适合小文件,字节数组不能太大的情况.(本章所用) B:通过DataHander的方式来进行传递. 1 ...

  3. c#实现gzip压缩解压缩算法:byte[]字节数组,文件,字符串,数据流的压缩解压缩

    转载:https://blog.csdn.net/luanpeng825485697/article/details/78165788 我测试了下压缩byte[],是可以的 using System; ...

  4. Java 中的字符串与 []byte 字节数组

    一.字符串 1.比较 String.HashSet.List 中的 contains 方法 其中, String.List 都使用了 indexOf 方法,本质是遍历,时间效率为 O(n).而 Has ...

  5. Image控件显示以byte[]字节数组形式存在的图片

    工作中遇到了这样的一个问题.起初觉得很简单,获得了图片的byte[]后,可以将其转换成内存中的图片对象(如System.Drawing.Image),而后赋给页面的Image控件.尝试后才发现这样根本 ...

  6. delphi中如何将string类型的字符串数据转化成byte[]字节数组类型的数据

    var  S:String;  P:PChar;  B:array of Byte;begin  S:='Hello';  SetLength(B,Length(S)+1);  P:=PChar(S) ...

  7. Java IO学习笔记(三)转换流、数据流、字节数组流

    转换流 1.转换流:将字节流转换成字符流,转换之后就可以一个字符一个字符的往程序写内容了,并且可以调用字符节点流的write(String s)方法,还可以在外面套用BufferedReader()和 ...

  8. 【Java】字节数组转换工具类

    import org.apache.commons.lang.ArrayUtils; import java.nio.charset.Charset; /** * 字节数组转换工具类 */ publi ...

  9. C#用Zlib压缩或解压缩字节数组

    /// <summary> /// 复制流 /// </summary> /// <param name="input">原始流</par ...

随机推荐

  1. git学习------> Gitlab如何进行备份恢复与迁移?

    前段时间,在某台CenterOS服务器上搭建了Gitlab环境,并且大家陆陆续续的都把代码从svn迁移到了gitlab,但是之前的CenterOS服务器并不是搭建在公司的机房环境,而是搭建在办公室的某 ...

  2. django模型:为已存在的表建立模型

    为已经存在的表建立模型:参考https://blog.csdn.net/opera95/article/details/78200024 为已经存在的表建立模型1.python manage.py i ...

  3. Swift学习——A Swift Tour 枚举和结构体

    版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/zhenyu5211314/article/details/28588095 Enumerations ...

  4. MySQL5.7密码安全策略(转)

    环境介绍:CentOS 6.7 MySQL版本:5.7.11 1.查看现有的密码策略 mysql> SHOW VARIABLES LIKE 'validate_password%';参数解释:1 ...

  5. python爬虫中文乱码解决方法

    python爬虫中文乱码 前几天用python来爬取全国行政区划编码的时候,遇到了中文乱码的问题,折腾了一会儿,才解决.现特记录一下,方便以后查看. 我是用python的requests和bs4库来实 ...

  6. 记录:tensoflow改错TypeError: Cannot interpret feed_dict key as Tensor: Can not convert a float into a Te

    错误描述: TypeError: Cannot interpret feed_dict key as Tensor: Can not convert a float into a Tensor. 改错 ...

  7. 搜狗员工用百度算什么,谷歌员工当着老板的面用bing,结果悲剧了!

    之前看到一篇文章,写的是搜狗的员工遇到问题时,用百度,结果网友的评论真是亮瞎眼.今天,W3Cschool小师妹将为大家分享一个类似的故事,那就是谷歌员工当着老板的面,竟然用BING. 这位谷歌员工称, ...

  8. 一步一步学EF系列三【数据迁移】

    我们每篇的内容都不多,所以希望在学习的过程中最后能亲自敲一下代码 这样更有利于掌握. 我们现在接着上篇的例子,我们现在给随便的表增加一个字段 CreateTime 创建日期 运行一下 看看会怎么样 修 ...

  9. -webkit-box

    父容器 display: flex; justify-content: center;/*主轴*/ align-items: center; /*交叉轴*/ display: -webkit-box; ...

  10. POJ - 3308 Paratroopers (最小点权覆盖)

    题意:N*M个格点,K个位置会有敌人.每行每列都有一门炮,能打掉这一行(列)上所有的敌人.每门炮都有其使用价值.总花费是所有使用炮的权值的乘积.求最小的总花费. 若每门炮的权值都是1,就是求最小点覆盖 ...