写入内容到文件

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. Mysql大数据量分页优化

    假设有一个千万量级的表,取1到10条数据: select * from table limit 0,10; select * from table limit 1000,10; 这两条语句查询时间应该 ...

  2. Upsource——对已签入的代码进行分享、讨论和审查代码

    Upsource 一.Upsource简介 Upsource ,这是一个专门为软件开发团队所设计的源代码协作工具.Upsource能够与多种版本控制工具进行集成,包括Git.Mercurial.Sub ...

  3. 20165324《Java程序设计》第四周

    学号 2016-2017-2 <Java程序设计>第四周学习总结 教材学习内容总结 第五章:子类与继承 子类的定义:class 子类名 extends 父类名 { ... } 子类继承性: ...

  4. 过滤adb logcat 日志

    原文地址http://blog.csdn.net/listening_music/article/details/7518990 另外比较好的文章http://blog.csdn.net/liao27 ...

  5. EventFiringWebDriver网页事件监听(一)

    Selenium提供了很多的event listening functions来跟踪脚本执行过程中的events. How it works? 在注册了listener的webDriver里面,这些l ...

  6. 360急速浏览器JS的调试

    1.代码中添加debugger关键字 2.360急速浏览器中选择工具--开发人员选项,如下图所示,在sources面板中可以看到正在运行的JS代码,F10可以单步函数执行,在鼠标放在变量上可以跟踪变量 ...

  7. 将jar包发布到nexus仓库

    版本的快速迭代不适合release发布到仓库,snapshot方便版本的快速迭代. 1.pom改为snapshot <dependency> <groupId>com.sf.c ...

  8. Docker+.Net Core 的那些事儿-1.准备工作

    1.下载centos 地址:https://www.centos.org/download/ 我使用的是DVD ISO,这么做的目的是为了在之后的docker填坑的路上,方便使用centos中Fire ...

  9. linux meta 18.0.1 系统安装nodejs

    前置条件是:需要准备sudo apt-get 命令 第一步: 执行命令sudo apt-get install nodejs 即可安装, 之后可使用node -v 查看版本node 版本号 第二步: ...

  10. Akka in action (第一章 介绍Akka)

    在本章 概述Akka 了解Actors和Actor系统 Akka的适用范围 在第一章中,会介绍给你Akk的个方面,它能做什么,与现有的解决方案有那些不同.重点关注Akka有哪些功能和使用范围和强大的并 ...