写入内容到文件

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. 对BeforeSuite和BeforeTest的理解

    在BeforeSuite.BeforeTest.BeforeClass.BeforeMethod及BeforeGroups中,后面三个注解都比较好理解,其实BeforeSuite.BeforeTest ...

  2. python学习笔记(十七)网络编程之urllib模块

    如何用python打开一个网站或者请求一个接口呢,我们在这篇博客介绍一下. 首先我们得导入一个urllib模块,这个模块是python自带的标准模块,直接导入就能使用,但是用起来不方便,先看个简单的打 ...

  3. 聊一聊python的单例模式

    单例模式 单例模式(Singleton Pattern)是一种常用的软件设计模式,该模式的主要目的是确保某一个类只有一个实例存在.当你希望在整个系统中,某个类只能出现一个实例时,单例对象就能派上用场. ...

  4. PHP生成名片、网址二维码

    PHP生成名片.网址二维码 php生成名片(vcard)二维码: <?php$vname = 'test';  $vtel = '13800000000';  generateQRfromGoo ...

  5. Error: UserWarning: Ignoring URL... 已解决

    数据data里存有url,用pandas的to_excel() 报错:UserWarning: Ignoring URL... 解决方案: 将 data.to_excel("data.xls ...

  6. 82. Remove Duplicates from Sorted List II(删除有序链表中的重复元素)

    Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numb ...

  7. Maven 在 IntelliJ IDEA 中的使用

    一.概述 Maven 为构建软件,与 Gradle 类似,也能以插件的方式在 IntelliJ IDEA 中得到使用. 同样地,你也可以配置环境变量,这样就能够在命令行中进行操作了. 二.使用方式 其 ...

  8. android线程学习心得

    有一篇关于android线程讲的非常好,大家可以参考下,其中有一句话讲的非常好,就拿来做开篇之句: 当一个程序第一次启动时,Android会同时启动一个对应的主线程(Main Thread),主线程主 ...

  9. Eclipse 导入Maven 项目报错

    新建Maven项目时出错:org.apache.maven.archiver.MavenArchiver.getManifest   新建Maven项目时出错:org.apache.maven.arc ...

  10. Tomcat之并发优化

    1.位置:      (1)/opt/tomcat7/conf下的server.xml文件中<Connector>节点的配置优化,记得先备份.      (2)出厂默认(在server.x ...