public
static
String compress(String str)
throws
IOException {
if
(
null
== str || str.length() <= 0
) {
return
str;
//
创建一个新的输出流
ByteArrayOutputStream out =
new
ByteArrayOutputStream();
//
使用默认缓冲区大小创建新的输出流
GZIPOutputStream gzip =
new
GZIPOutputStream(out);
//
将字节写入此输出流
gzip.write(str.getBytes("utf-8"));
//
因为后台默认字符集有可能是GBK字符集,所以此处需指定一个字符集
gzip.close();
//
使用指定的 charsetName,通过解码字节将缓冲区内容转换为字符串
return
out.toString("ISO-8859-1"
);
* 字符串的解压
*
@param
str
* 对字符串解压
*
@return
返回解压缩后的字符串
*
@throws
IOException
public
static
String unCompress(String str)
throws
IOException {
if
(
null
== str || str.length() <= 0
) {
return
str;
//
创建一个新的输出流
ByteArrayOutputStream out =
new
ByteArrayOutputStream();
//
创建一个 ByteArrayInputStream,使用 buf 作为其缓冲区数组
ByteArrayInputStream in =
new
ByteArrayInputStream(str.getBytes("ISO-8859-1"
));
//
使用默认缓冲区大小创建新的输入流
GZIPInputStream gzip =
new
GZIPInputStream(in);
byte
[] buffer =
new
byte
[256
];
int
n = 0
;
//
将未压缩数据读入字节数组
while
((n = gzip.read(buffer)) >= 0
) {
out.write(buffer,
0
, n);
//
使用指定的 charsetName,通过解码字节将缓冲区内容转换为字符串
return
out.toString("utf-8"
);
public
static
void
main(String[] args)
throws
IOException {
String str
="看甲方时点击翻身肯\n";
//
内容大小控制在240byte, >240 进行压缩·否则不压··
System.out.println("原文大小:"+str.getBytes().length+" \n压缩前:"+
str);
String compress
=
GZIP.compress(str);
System.out.println(
"解压大小:"+compress.getBytes().length+" \n压缩后:"+
compress);
String uncompress
=
GZIP.unCompress(compress);
System.out.println(
"解压大小:"+uncompress.getBytes().length+" \n解压缩:"+
uncompress);
转载于:https://www.cnblogs.com/94xiyang/p/10245034.html
public class GZIP { /** * 字符串的压缩 * * @param str * 待压缩的字符串 * @return 返回压缩后的字符串 * @throws IOException */ public static String compress(S...
gzip
是GNUzip的缩写,它是一个GNU自由软件的文件
压缩
程序。
HTTP协议上的
GZIP
编码是一种用来改进WEB应用程序性能的技术。一般服务器中都安装有这个功能模块的,服务器端不需做改动。
当浏览器支持
gzip
格式的时候, 服务器端会传输
gzip
格式的数据。
从Http 技术细节上讲,就是 http request 头中 有 “Accept-Encoding”, “
gzip
” ,response 中就有返回头Content-Encoding=
gzip
我们现在从浏览器上访问玩啥网站都是
gzip
格式传输的。
但是我们现在
Android
客户端,没有用
Gzip
http数据传输,body
压缩
。节省流量
Android
开发中
网络
请求的
压缩
──
GZip
的使用
http client对post内容
gzip
压缩
和server端
解压
接收
Android
中使用
gzip
传递数据以上是参考资料下面是我测试效果fiddler 抓包代码public static void reqeust(String pUrl) {
URL url;
public static final String
GZIP
_ENCODE_UTF_8 = "UTF-8";
public static final String
GZIP
_ENCODE_ISO_8859_1 = "ISO-8859-1";
private static final char ARRAY_LEFT = ...
进行安卓的软件开发就少不了和
网络
上的数据打交道,为了做出让用户喜爱的软件,我们需要想一切办法减少下载流量,加快下载速度。
GZIP
就是现有的一种网站
压缩
格式,只要网站支持,我们就可以从网站上下载
压缩
过的数据包。
GZIP
的介绍请看百度百科:http://baike.baidu.com/view/966625.htm
启用
GZIP
:
启用
GZIP
通信需要服务器和客户端双方的
说明:现在很多网站都会在回传数据的时候进行
GZIP
压缩
,我们可以在请求头中申明支持
GZIP
压缩
。可以减轻
网络
传输压力,Xutils中已经实现。
下面是一个DEMO,便于理解。
private void init
Gzip
() {
findViewById(R.id.btn1).setOnClickListener(new OnClickListener() {
import java.io.*;
import java.util.zip.
GZIP
InputStream;
import java.util.zip.
GZIP
OutputStream;
public class
Gzip
Util {
public static byte[] compress(byte[] data) throws IOException {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
GZIP
OutputStream
gzip
= new
GZIP
OutputStream(bos);
gzip
.write(data);
gzip
.finish();
gzip
.close();
return bos.toByteArray();
public static byte[] decompress(byte[] data) throws IOException {
ByteArrayInputStream bis = new ByteArrayInputStream(data);
GZIP
InputStream
gzip
= new
GZIP
InputStream(bis);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int len;
while ((len =
gzip
.read(buffer)) != -1) {
bos.write(buffer, 0, len);
gzip
.close();
return bos.toByteArray();
代码中的 `compress` 方法实现了
gzip
压缩
,而 `decompress` 方法则实现了
gzip
解压
。
### 回答2:
可以使用Java的
GZIP
InputStream和
GZIP
OutputStream类来实现
gzip
压缩
和
解压
。
下面是一个用Java编写的简单示例代码:
import java.io.*;
import java.util.zip.
GZIP
InputStream;
import java.util.zip.
GZIP
OutputStream;
public class
Gzip
Example {
public static void main(String[] args) {
String inputFilePath = "/path/to/inputfile.txt";
String compressedFilePath = "/path/to/compressedfile.gz";
String decompressedFilePath = "/path/to/decompressedfile.txt";
//
压缩
文件
compressFile(inputFilePath, compressedFilePath);
//
解压
文件
decompressFile(compressedFilePath, decompressedFilePath);
public static void compressFile(String inputFilePath, String compressedFilePath) {
try {
FileInputStream fis = new FileInputStream(inputFilePath);
FileOutputStream fos = new FileOutputStream(compressedFilePath);
GZIP
OutputStream
gzip
OS = new
GZIP
OutputStream(fos);
byte[] buffer = new byte[1024];
int length;
while ((length = fis.read(buffer)) != -1) {
gzip
OS.write(buffer, 0, length);
gzip
OS.close();
fos.close();
fis.close();
System.out.println("文件已成功
压缩
为
gzip
格式!");
} catch (IOException e) {
e.printStackTrace();
public static void decompressFile(String compressedFilePath, String decompressedFilePath) {
try {
FileInputStream fis = new FileInputStream(compressedFilePath);
GZIP
InputStream
gzip
IS = new
GZIP
InputStream(fis);
FileOutputStream fos = new FileOutputStream(decompressedFilePath);
byte[] buffer = new byte[1024];
int length;
while ((length =
gzip
IS.read(buffer)) != -1) {
fos.write(buffer, 0, length);
fos.close();
gzip
IS.close();
fis.close();
System.out.println("文件已成功
解压
缩!");
} catch (IOException e) {
e.printStackTrace();
使用上述代码,你可以将指定的输入文件
压缩
为
gzip
格式的文件,并将其
解压
缩为原始文件。只需将"inputFilePath","compressedFilePath"和"decompressedFilePath"变量设置为你希望的文件路径即可。请确保在运行代码之前将这些路径更改为实际路径。
### 回答3:
Sure! 下面是用Java编写的一个使用
GZIP
压缩
和
解压
缩的例子:
压缩
代码:
```java
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.zip.
GZIP
OutputStream;
public class
Gzip
CompressionExample {
public static void main(String[] args) {
String sourceFile = "input.txt";
String
gzip
File = "compressed.gz";
try (FileInputStream fis = new FileInputStream(sourceFile);
FileOutputStream fos = new FileOutputStream(
gzip
File);
GZIP
OutputStream
gzip
OS = new
GZIP
OutputStream(fos)) {
byte[] buffer = new byte[1024];
int bytesRead;
while ((bytesRead = fis.read(buffer)) != -1) {
gzip
OS.write(buffer, 0, bytesRead);
System.out.println("File compressed successfully!");
} catch (IOException e) {
e.printStackTrace();
解压
代码:
```java
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.zip.
GZIP
InputStream;
public class
Gzip
DecompressionExample {
public static void main(String[] args) {
String
gzip
File = "compressed.gz";
String outputFile = "output.txt";
try (FileInputStream fis = new FileInputStream(
gzip
File);
GZIP
InputStream
gzip
IS = new
GZIP
InputStream(fis);
FileOutputStream fos = new FileOutputStream(outputFile)) {
byte[] buffer = new byte[1024];
int bytesRead;
while ((bytesRead =
gzip
IS.read(buffer)) != -1) {
fos.write(buffer, 0, bytesRead);
System.out.println("File decompressed successfully!");
} catch (IOException e) {
e.printStackTrace();
以上代码基于Java 8的try-with-resources语法实现了
GZIP
压缩
和
解压
缩。需要注意的是,
压缩
文件中需要进行
解压
的数据的大小会比原始文件更小。另外,确保输入文件存在,并提供正确的文件路径。
希望这可以帮到你!如果你对这方面还有什么疑问,欢迎继续提问哦!
We're sorry but mobile doesn't work properly without JavaScript enabled. Please enable it to continu...
20912