1. 程式人生 > >Android-工作遭遇-壓縮傳輸資料並且防止抓包

Android-工作遭遇-壓縮傳輸資料並且防止抓包

在開發中,遇到了位元組過大,以及非常容易被抓包,這種時候,進行壓縮了位元組陣列,並且大小減小,功能展現非常強大

效果圖.上面的文字是1022大小壓縮前的文字,下面的文字是311大小解壓出來的文字.沒有任何的資料丟失

這邊主要是採用了lz4的字串壓縮技術,進行壓縮與解壓,只要跟後端約定,就可以實現,

如果封裝了請求網路的框架,則只用在新增資料的時候加上處理即可.

附上工具類原始碼

public class Lz4Util {
	
	/**
     * @param srcByte 原始資料
     * @return 壓縮後的資料
     */
    public static byte[] compressedByte(byte[] srcByte) {
        LZ4Factory factory = LZ4Factory.fastestInstance();
        LZ4Compressor compressor = factory.fastCompressor();
        return compressor.compress(srcByte);
    }

    /**
     * @param compressorByte 壓縮後的資料
     * @param srcLength      壓縮前的資料長度
     * @return
     */
    public static byte[] decompressorByte(byte[] compressorByte, int srcLength) {
        LZ4Factory factory = LZ4Factory.fastestInstance();
        LZ4FastDecompressor decompressor = factory.fastDecompressor();
        return decompressor.decompress(compressorByte, srcLength);
    }

    /**
     * @param srcByte
     * @param blockSize 一次壓縮的大小 取值範圍 64 位元組-32M之間
     * @return
     * @throws IOException
     */
    public static byte[] lz4Compress(byte[] srcByte, int blockSize) throws IOException {
        LZ4Factory factory = LZ4Factory.fastestInstance();
        ByteArrayOutputStream byteOutput = new ByteArrayOutputStream();
        LZ4Compressor compressor = factory.fastCompressor();
        LZ4BlockOutputStream compressedOutput = new LZ4BlockOutputStream(byteOutput, blockSize, compressor);
        compressedOutput.write(srcByte);
        compressedOutput.close();
        return byteOutput.toByteArray();
    }

    /**
     * @param compressorByte
     * @param blockSize      一次壓縮的大小 取值範圍 64 位元組-32M之間
     * @return
     * @throws IOException
     */
    public static byte[] lz4Decompress(byte[] compressorByte, int blockSize) throws IOException {
        LZ4Factory factory = LZ4Factory.fastestInstance();
        ByteArrayOutputStream baos = new ByteArrayOutputStream(blockSize);
        LZ4FastDecompressor decompresser = factory.fastDecompressor();
        LZ4BlockInputStream lzis = new LZ4BlockInputStream(new ByteArrayInputStream(compressorByte), decompresser);
        int count;
        byte[] buffer = new byte[blockSize];
        while ((count = lzis.read(buffer)) != -1) {
            baos.write(buffer, 0, count);
        }
        lzis.close();
        return baos.toByteArray();
    }

    /**
     * File  to byte[]
     *
     * @param filePath
     * @return
     * @throws IOException
     */
    public static byte[] returnFileByte(String filePath) throws IOException {
        FileInputStream fileInputStream = new FileInputStream(new File(filePath));
        FileChannel channel = fileInputStream.getChannel();
        ByteBuffer byteBuffer = ByteBuffer.allocate((int) channel.size());
        channel.read(byteBuffer);
        return byteBuffer.array();
    }

    /**
     * createFile
     *
     * @param fileByte
     * @param filePath
     */
    public static void createFile(byte[] fileByte, String filePath) {
        BufferedOutputStream bufferedOutputStream;
        FileOutputStream fileOutputStream;
        File file = new File(filePath);
        try {
            fileOutputStream = new FileOutputStream(file);
            bufferedOutputStream = new BufferedOutputStream(fileOutputStream);
            bufferedOutputStream.write(fileByte);
            fileOutputStream.close();
            bufferedOutputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    
    public static void main(String[] args) throws Exception{
		String testString = "sdfssdfsdfsdsdfsdffsddfasdfsfsdfpowpeori293402384029''ds;flsdjwelkjwlkjf2849849234usdkjfskfnsdnfafhwjkehnsmdnf284924583weijrwkjfkfdlsdjfksdlsdsdfsfkwdjwelkjwlkjf2849849234usdkjfskfnsdnfafhwjkehnsmdnf284924583weijrwkjfkfdlsdjfksdlsdsdfsfkwdjwelkjwlkjf2849849234usdkjfskfnsdnfafhwjkehnsmdnf284924583weijrwkjfkfdlsdjfksdlsdsdfsfkwdjwelkjwlkjf2849849234usdkjfskfnsdnfafhwjkehnsmdnf284924583weijrwkjfkfdlsdjfksdlsdsdfsfkwdjwelkjwlkjf2849849234usdkjfskfnsdnfafhwjkehnsmdnf284924583weijrwkjfkfdlsdjfksdlsdsdfsfkwdjwelkjwlkjf2849849234usdkjfskfnsdnfafhwjkehnsmdnf284924583weijrwkjfkfdlsdjfksdlsdsdfsfkwdjwelkjwlkjf2849849234usdkjfskfnsdnfafhwjkehnsmdnf284924583weijrwkjfkfdlsdjfksdlsdsdfsfkwdjwelkjwlkjf2849849234usdkjfskfnsdnfafhwjkehnsmdnf284924583weijrwkjfkfdlsdjfksdlsdsdfsfkw;djwelkjwlkjf2849849234usdkjfskfnsdnfafhwjkehnsmdnf284924583weijrwkjfkfdlsdjfksdlsdsdfsfkw;le;wjlksjfsjfkwjeoiwoierjpjsldkfjnldnmv,xncvmxnvcklejrowiejrlknsdf,smnm,ncskjsdfoilkrw;kj238479283lsndkfl29o234klnsldfksjklk slkdfjlsfjslfjskekeksfjlskfslfksef";


		System.out.println(testString);
		
		byte[] bytes = testString.getBytes();
		
		System.out.println(bytes.length);
		
		byte[] desBytes = Lz4Util.compressedByte(bytes);
		
		System.out.println(desBytes.length);
		
		byte[] finalBytes = Lz4Util.decompressorByte(desBytes, bytes.length);
		
		System.out.println(new String(finalBytes, "utf-8"));
    	
	}

}

本來想放jar包,好像無法上傳