byte字節數組的壓縮

寫入內容到文件java

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文件 url

 

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文件 spa

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文件code

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;
    }
相關文章
相關標籤/搜索