byte字节数组的压缩

写入内容到文件

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;
}
上一篇:关于navicat连接oracle 报 ORA-12737 set CHS16GBK错误的解决方案


下一篇:java进阶视频分享