byteバイト配列の圧縮
14628 ワード
ファイルへの書き込み
gzip圧縮byte[]
zip圧縮byte[]
byte配列に基づいてtxtファイルを生成する
byte配列に基づいてzipファイルを生成する
HttpGet取得バイト配列はzipに圧縮する.tar.gzファイル
転載先:https://www.cnblogs.com/learnapi/p/9815343.html
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;
}
転載先:https://www.cnblogs.com/learnapi/p/9815343.html