当前位置:首页 » 文件管理 » gzip解压java

gzip解压java

发布时间: 2022-07-05 14:45:34

❶ 是否能用delphi的zlib解压java gzip压缩的字符串

可以使用 delphi 与 java 完成数据压缩还原的交通。
不管是 java还是 delphi,算法都有现成的控件,关键是要使用同样的压缩协议。请参考以下资料:
在Java与Delphi间交互实现Zlib压缩算法
http://blog.csdn.net/hexingyeyun/article/details/8678154

❷ 在java中,gzip 压缩和解压多个文件

直接编译运行!!!

不知道你是要查看压缩文件还是要解压文件,所以发上来两个。
第一个可以查看各个压缩项目;
第二个可以解压文件。

import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.*;
import java.util.zip.*;
import javax.swing.*;
import javax.swing.filechooser.FileFilter;

class ZipTest {
public static void main(String[] args) {
ZipTestFrame frame = new ZipTestFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}

class ZipTestFrame extends JFrame {

private JComboBox fileCombo;
private JTextArea fileText;
private String zipname;

public ZipTestFrame() {

setTitle("ZipTest");
setSize(400,300);

JMenuBar menuBar = new JMenuBar();
JMenu menu = new JMenu("File");

JMenuItem openItem = new JMenuItem("Open");
menu.add(openItem);
openItem.addActionListener(new OpenAction());

JMenuItem exitItem = new JMenuItem("Exit");
menu.add(exitItem);
exitItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
System.exit(0);
}
});

menuBar.add(menu);
setJMenuBar(menuBar);

fileText = new JTextArea();
fileCombo = new JComboBox();
fileCombo.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
loadZipFile((String)fileCombo.getSelectedItem());
}
});
add(fileCombo, BorderLayout.SOUTH);
add(new JScrollPane(fileText), BorderLayout.CENTER);
}

public class OpenAction implements ActionListener {
public void actionPerformed(ActionEvent event) {
JFileChooser chooser = new JFileChooser();
chooser.setCurrentDirectory(new File("."));
ExtensionFileFilter filter = new ExtensionFileFilter();
filter.addExtension(".zip");
filter.addExtension(".jar");
filter.setDescription("ZIP archives");
chooser.setFileFilter(filter);
int r = chooser.showOpenDialog(ZipTestFrame.this);
if(r == JFileChooser.APPROVE_OPTION) {
zipname = chooser.getSelectedFile().getPath();
scanZipFile();
}
}
}

public void scanZipFile() {
fileCombo.removeAllItems();
try {
ZipInputStream zin = new ZipInputStream(new FileInputStream(zipname));
ZipEntry entry;
while((entry = zin.getNextEntry()) != null) {
fileCombo.addItem(entry.getName());
zin.closeEntry();
}
zin.close();
} catch(IOException e) {
e.printStackTrace();
}
}

public void loadZipFile(String name) {
try {
ZipInputStream zin = new ZipInputStream(new FileInputStream(zipname));
ZipEntry entry;
fileText.setText("");

while((entry = zin.getNextEntry()) != null) {
if(entry.getName().equals(name)) {
BufferedReader in = new BufferedReader(new InputStreamReader(zin));
String line;
while((line = in.readLine())!=null) {
fileText.append(line);
fileText.append("\n");
}
}
zin.closeEntry();
}
zin.close();
} catch(IOException e) {
e.printStackTrace();
}
}
}

class ExtensionFileFilter extends FileFilter {

private String description = "";
private ArrayList<String>extensions = new ArrayList<String>();

public void addExtension(String extension) {
if(!extension.startsWith("."))
extension = "." + extension;
extensions.add(extension.toLowerCase());
}

public void setDescription(String aDescription) {
description = aDescription;
}

public String getDescription() {
return description;
}

public boolean accept(File f) {
if(f.isDirectory()) return true;
String name = f.getName().toLowerCase();

for(String e : extensions)
if(name.endsWith(e))
return true;
return false;
}
}

///////////////////////////////////////////////////////////
/**
*类名:zipFileRelease
*说明:一个zip文件解压类
*介绍:主要的zip文件释放方法releaseHandle()
* 用ZipInputStream类和ZipEntry类将zip文件的入口清单列举出来,然后
* 根据用户提供的输出路径和zip文件的入口进行组合通过DataOutputStream
* 和File类进行文件的创建和目录的创建,创建文件时的文件数据是通过
* ZipInputStream类、ZipEntry类、InputStream类之间的套嵌组合获得的。
*注意:如果zip文件中包含中文路径程序将会抛出异常
*/

import java.io.*;
import java.util.*;
import java.util.zip.*;

class zipFileRelease{

private String inFilePath;
private String releaseFilePath;
private String[] FileNameArray; //存放文件名称的数组
private ZipEntry entry;
//
private FileInputStream fileDataIn;
private FileOutputStream fileDataOut;
private ZipInputStream zipInFile;
private DataOutputStream writeData;
private DataInputStream readData;
//
private int zipFileCount = 0; //zip文件中的文件总数
private int zipPathCount = 0; //zip文件中的路径总数

/**
*初始化函数
*初始化zip文件流、输出文件流以及其他变量的初始化
*/
public zipFileRelease(String inpath,String releasepath){
inFilePath = inpath;
releaseFilePath = releasepath;
}

/**
*初始化读取文件流函数
*参数:FileInputStream类
*返回值:初始化成功返回0,否则返回-1
*/
protected long initInStream(ZipInputStream zipFileA){
try{
readData = new DataInputStream(zipFileA);
return 0;
}catch(Exception e){
e.printStackTrace();
return -1;
}
}

/**
*测试文件路径
*参数:zip文件的路径和要释放的位置
*返回值:是两位整数,两位数中的十位代表输入路径和输出路径(1输入、2输出)
* 各位数是代表绝对路径还是相对路径(1绝对、0相对)
* 返回-1表示路径无效

protected long checkPath(String inPath,String outPath){
File infile = new File(inPath);
File infile = new File(outPath);

}
*/

/**
*初始化输出文件流
*参数:File类
*返回值:初始化成功返回0,否则返回-1
*/
protected long initOutStream(String outFileA){
try{
fileDataOut = new FileOutputStream(outFileA);
writeData = new DataOutputStream(fileDataOut);
return 0;
}catch(IOException e){
e.printStackTrace();
return -1;
}
}

/**
*测试文件是否存在方法
*参数:File类
*返回值:如果文件存在返回文件大小,否则返回-1
*/
public long checkFile(File inFileA){
if (inFileA.exists()){
return 0;
}else{
return -1;
}
}

/**
*判断文件是否可以读取方法
*参数:File类
*返回值:如果可以读取返回0,否则返回-1
*/
public long checkOpen(File inFileA){
if(inFileA.canRead()){
return inFileA.length();
}else{
return -1;
}
}

/**
*获得zip文件中的文件夹和文件总数
*参数:File类
*返回值:如果正常获得则返回总数,否则返回-1
*/
public long getFilFoldCount(String infileA){
try{
int fileCount = 0;
zipInFile = new ZipInputStream(new FileInputStream(infileA));
while ((entry = zipInFile.getNextEntry()) != null){
if (entry.isDirectory()){
zipPathCount++;
}else{
zipFileCount++;
}
fileCount++;
}
return fileCount;
}catch(IOException e){
e.printStackTrace();
return -1;
}
}

/**
*读取zip文件清单函数
*参数:File类
*返回值:文件清单数组
*/
public String[] getFileList(String infileA){
try{
ZipInputStream AzipInFile = new ZipInputStream(new FileInputStream(infileA));
//创建数组对象
FileNameArray = new String[(int)getFilFoldCount(infileA)];

//将文件名清单传入数组
int i = 0;
while ((entry = AzipInFile.getNextEntry()) != null){
FileNameArray[i++] = entry.getName();
}
return FileNameArray;
}catch(IOException e){
e.printStackTrace();
return null;
}
}

/**
*创建文件函数
*参数:File类
*返回值:如果创建成功返回0,否则返回-1
*/
public long writeFile(String outFileA,byte[] dataByte){
try{
if (initOutStream(outFileA) == 0){
writeData.write(dataByte);
fileDataOut.close();
return 0;
}else{
fileDataOut.close();
return -1;
}
}catch(IOException e){
e.printStackTrace();
return -1;
}
}

/**
*读取文件内容函数
*参数:File类
*返回值:如果读取成功则返回读取数据的字节数组,如果失败则返回空值
*/
protected byte[] readFile(ZipEntry entryA,ZipInputStream zipFileA){
try{
long entryFilelen;
if (initInStream(zipFileA) == 0){
if ((entryFilelen = entryA.getSize()) >= 0){
byte[] entryFileData = new byte[(int)entryFilelen];
readData.readFully(entryFileData,0,(int)entryFilelen);
return entryFileData;
}else{
return null;
}
}else{
return null;
}
}catch(IOException e){
e.printStackTrace();
return null;
}
}

/**
*创建目录函数
*参数:要创建目录的路径
*返回值:如果创建成功则返回0,否则返回-1
*/
public long createFolder(String dir){
File file = new File(dir);
if (file.mkdirs()) {
return 0;
}else{
return -1;
}
}

/**
*删除文件
*参数:要删除的文件
*返回值:如果删除成功则返回0,要删除的文件不存在返回-2
* 如果要删除的是个路径则返回-3,删除失败则返回-1
*/
public long deleteFile(String Apath) throws SecurityException {
File file = new File(Apath.trim());
//文件或路径不存在
if (!file.exists()){
return -2;
}
//要删除的是个路径
if (!file.isFile()){
return -3;
}
//删除
if (file.delete()){
return 0;
}else{
return -1;
}
}

/**
*删除目录
*参数:要删除的目录
*返回值:如果删除成功则返回0,删除失败则返回-1
*/
public long deleteFolder(String Apath){
File file = new File(Apath);
//删除
if (file.delete()){
return 0;
}else{
return -1;
}
}

/**
*判断所要解压的路径是否存在同名文件
*参数:解压路径
*返回值:如果存在同名文件返回-1,否则返回0
*/
public long checkPathExists(String AreleasePath){
File file = new File(AreleasePath);
if (!file.exists()){
return 0;
}else{
return -1;
}
}

/**
*删除zip中的文件
*参数:文件清单数组,释放路径
*返回值:如果删除成功返回0,否则返回-1
*/
protected long deleteReleaseZipFile(String[] listFilePath,String releasePath){
long arrayLen,flagReturn;
int k = 0;
String tempPath;
//存放zip文件清单的路径
String[] pathArray = new String[zipPathCount];
//删除文件
arrayLen = listFilePath.length;
for(int i=0;i<(int)arrayLen;i++){
tempPath = releasePath.replace('\\','/') + listFilePath[i];
flagReturn = deleteFile(tempPath);
if (flagReturn == -2){
//什么都不作
}else if (flagReturn == -3){
pathArray[k++] = tempPath;
}else if (flagReturn == -1){
return -1;
}
}
//删除路径
for(k = k - 1;k>=0;k--){
flagReturn = deleteFolder(pathArray[k]);
if (flagReturn == -1) return -1;
}
return 0;
}

/**
*获得zip文件的最上层的文件夹名称
*参数:zip文件路径
*返回值:文件夹名称,如果失败则返回null
*/
public String getZipRoot(String infileA){
String rootName;
try{
FileInputStream tempfile = new FileInputStream(infileA);
ZipInputStream AzipInFile = new ZipInputStream(tempfile);
ZipEntry Aentry;
Aentry = AzipInFile.getNextEntry();
rootName = Aentry.getName();
tempfile.close();
AzipInFile.close();
return rootName;
}catch(IOException e){
e.printStackTrace();
return null;
}
}

/**
*释放流,释放占用资源
*/
protected void closeStream() throws Exception{
fileDataIn.close();
fileDataOut.close();
zipInFile.close();
writeData.flush();
}

/**
*解压函数
*对用户的zip文件路径和解压路径进行判断,是否存在和打开
*在输入解压路径时如果输入"/"则在和zip文件存放的统计目录下进行解压
*返回值:0表示释放成功
* -1 表示您所要解压的文件不存在、
* -2表示您所要解压的文件不能被打开、
* -3您所要释放的路径不存在、
* -4您所创建文件目录失败、
* -5写入文件失败、
* -6表示所要释放的文件已经存在、
* -50表示文件读取异常
*/
public long releaseHandle() throws Exception{
File inFile = new File(inFilePath);
File outFile = new File(releaseFilePath);
String tempFile;
String zipPath;
String zipRootPath;
String tempPathParent; //存放释放路径
byte[] zipEntryFileData;

//作有效性判断
if (checkFile(inFile) == -1) {
return -1;}
if (checkOpen(inFile) == -1) {
return -2;}
//不是解压再当前目录下时对路径作有效性检验
if (!releaseFilePath.equals("/")){
//解压在用户指定目录下
if (checkFile(outFile) == -1) {
return -3;}
}
//获得标准释放路径
if (!releaseFilePath.equals("/")) {
tempPathParent = releaseFilePath.replace('\\','/')+ "/";
}else{
tempPathParent = inFile.getParent().replace('\\','/')+ "/";
}
//获得zip文件中的入口清单
FileNameArray = getFileList(inFilePath);
//获得zip文件的最上层目录
zipRootPath = getZipRoot(inFilePath);
//
fileDataIn = new FileInputStream(inFilePath);
zipInFile = new ZipInputStream(fileDataIn);
//判断是否已经存在要释放的文件夹
if (zipRootPath.lastIndexOf("/") > 0 ){
if (checkPathExists(tempPathParent +
zipRootPath.substring(0,zipRootPath.lastIndexOf("/"))) == -1){
return -6;
}
}else{
if (checkPathExists(tempPathParent + zipRootPath) == -1){
return -6;
}
}

//
try{
//创建文件夹和文件
int i = 0;
while ((entry = zipInFile.getNextEntry()) != null){
if (entry.isDirectory()){
//创建目录
zipPath = tempPathParent + FileNameArray[i];
zipPath = zipPath.substring(0,zipPath.lastIndexOf("/"));
if (createFolder(zipPath) == -1){
closeStream();
deleteReleaseZipFile(FileNameArray,tempPathParent);
return -4;
}

}else{
//读取文件数据
zipEntryFileData = readFile(entry,zipInFile);
//向文件写数据
tempFile = tempPathParent + FileNameArray[i];
//写入文件
if (writeFile(tempFile,zipEntryFileData) == -1){
closeStream();
deleteReleaseZipFile(FileNameArray,tempPathParent);
return -5;
}
}
i++;
}
//释放资源
closeStream();
return 0;
}catch(Exception e){
closeStream();
deleteReleaseZipFile(FileNameArray,tempPathParent);
e.printStackTrace();
return -50;
}
}
/**
*演示函数
*根据用户输入的路径对文件进行解压
*/
public static void main(String args[]) throws Exception {

long flag; //返回标志
String inPath,releasePath;

//获得用户输入信息
BufferedReader userInput = new BufferedReader(
new InputStreamReader(System.in));
System.out.println("请输入zip文件路径:");
inPath = userInput.readLine();
System.out.println("请输入保存路径:");
releasePath = userInput.readLine();
userInput.close();

//执行解压缩
zipFileRelease pceraZip = new zipFileRelease(inPath,releasePath);
flag = pceraZip.releaseHandle();

//出错信息打印
if (flag == 0) System.out.println("释放成功!!!");
if (flag == -1) System.out.println("您所要解压的文件不存在!");
if (flag == -2) System.out.println("您所要解压的文件不能被打开!");
if (flag == -3) System.out.println("您所要释放的路径不存在!");
if (flag == -4) System.out.println("您所创建文件目录失败!");
if (flag == -5) System.out.println("写入文件失败!");
if (flag == -6) System.out.println("文件已经存在!");
if (flag == -50) System.out.println("文件读取异常!");
}
}

❸ C# 中GZIP 压缩,求在JAVA中解压代码

byte[] buf = new byte[4096*2];
//建立字节数组输入流
ByteArrayInputStream i = new ByteArrayInputStream(buffer);
//建立gzip解压输入流
GZIPInputStream gzin = new GZIPInputStream(i);
int size = gzin.read(buf);
i.close();
gzin.close();
byte b[] = new byte[size];
System.array(buf,0,b,0,size);
return b;

❹ java怎么用Gzip实现文件的压缩和解压缩的

代码:

❺ java程序如何批量解压GZIP压缩包

给你一段单个文件解压gzip文件代码
批量解压的话 File f = new File("要解压的文件夹目录");
String paths[] = f.list(); // 取得文件夹下的文件

然后循环调用下面的方法就可以了。

try {
// Open the compressed file
String inFilename = "infile.gzip";
GZIPInputStream in = new GZIPInputStream(new FileInputStream(inFilename));

// Open the output file
String outFilename = "outfile";
OutputStream out = new FileOutputStream(outFilename);

// Transfer bytes from the compressed file to the output file
byte[] buf = new byte[1024];
int len;
while ((len = in.read(buf)) > 0) {
out.write(buf, 0, len);
}

// Close the file and stream
in.close();
out.close();
} catch (IOException e) {
}

❻ JAVA 与 c# GZIp

听他瞎吹,关于Java方面我不了解,但是对于.net方面,使用GZip进行压缩的并没有在前边多出4个字节!我也很奇怪,所以进行了测试,(我测试了4.0和4.52两个版本)
至于为什么是四个字节,非固定块加密时(固定块表示每块长度固定),一般要说明其块长度,而这个一般使用的是4个字节的整数(.net中的Int32/int),不同语言的可能定义也不同,但这个4个字符来源于GZip的规定用四个字节来说明长度(低位在前,高位在后)。所以不管是哪个语言,不管4个字节是叫int(.net)还是叫long(C++)都必须使用4个字节的。
但从头到尾均没有出现在前边有四个字节的说法!且我也没有听说过,因为GZip是一种压缩算法,这种算法是否在前边存在有字节长度的说明,是算法规定好的!所以我的第一反应就是不可能的——GZip有自己的标准,难道某个语言实现时不按标准来吗?
那么是不是有可能出现两种语言无法解压的情况呢?标准之所以称之为标准,两种语言实现时必须按相同的标准,换句话来说标准本身就必须在不同的语言实现达到一个统一转换的过程。不可能用.net的MD5到Java中验证不了,也不可能.net的GZip压缩到Java中解压不了!
那么为什么会出现某些人说的无法解压的情况呢?因为死读书的程序员导致的!标准是标准,但标准中也往往有不同的选择!比如在GZip压缩中就有快速/优化两种压缩方式,当然这两处方式。而不在同语言中所使用的默认压缩方式不同,另一种语言中的默认解压方式不同,就会出现无法匹配的情况。
我举个例子吧,好多人在问DES加解密Java与.net不对称,我觉得不可能,后来才知道,一边默认压缩,另一连默认解压,问题在于.net默认是CBC格式,而Java中恰恰不是!这就导致加解密错误的原因。事实上我即使在不跨语言时也会经常指明格式,所以我倒是没有遇到这种情况。
比如 GZipStream stream = new GZipStream(baseStream, CompressLevel.Faster,true);试试这个,但不管怎么说,你若去掉四个字节,在Android中的出错就表示,其实你去错了!

❼ 如何解压java 压缩的 gzip字符串

一个zip可以内藏多个文件
狭义的gzip仅对单个文件压缩,不能打包多个文件。
tar.gzip或tgz可以打包多个文件,属于固实压缩,压缩比较高,但随机存取单个文件的效率不如zip..

❽ java端用GZIPOutputStream压缩的数据,通过HTTP POST到PHP写的后台,怎么不能解压

GZIPOutputStream和PHP的gzuncompress配合得不好,似乎是Java产生的数据头在PHP那边认不出来。用DeflaterOutputStream来取代GZIPOutputStream。

❾ 如何在java REST API中用GZip和Jersey压缩相应

有许多情景当你的REST api提供的相应是非常长的,并且我们都知道传递速度和贷款在移动设备/网络上是多重要。当开发支持REST apis的移动app的时候,我认为首要的性能最优化的点就是需要解决。猜猜是什么?因为响应式文本,因此我们能压缩这些文本。而且随着当前的只能手机和平板的能力,在客户端解压文本应该不是个大问题...因此在这篇文章中,如果你使用java的Jersey构建它,我将介绍你怎么能有选择性的压缩REST API响应,这个Jersey事JAX-RS的映射实现(还有更多)...

1.Jersey过滤器和拦截器

啊,感谢Jersey的强大的过滤器和拦截器特性,这个实现是相当容易的。然后过滤器是主要打算来维护像HTTP headers,URIs和/或HTTP methods的request和response的参数,拦截器是维护实体,通过维护实体的输入/输出流。

但是对于压缩将使用一个GZip WriterInterceptor,一个写拦截器被用于这种情况,在那个类里,实体被写到"wire",当在这种情况中时,它在服务器这边,这就意味着输出一个响应实体。

1.1GZip Writer Interceptor

那让我们来看看我们的GZip Writer Interceptor吧:

GZip Writer Interceptor

package org.codingpedia.demo.rest.interceptors;

import java.io.IOException;

import java.io.OutputStream;

import java.util.zip.GZIPOutputStream;

import javax.ws.rs.WebApplicationException;

import javax.ws.rs.core.MultivaluedMap;

import javax.ws.rs.ext.WriterInterceptor;

import javax.ws.rs.ext.WriterInterceptorContext;

@Provider

@Compress

public class GZIPWriterInterceptor implements WriterInterceptor {

@Override

public void aroundWriteTo(WriterInterceptorContext context)

throws IOException, WebApplicationException {

MultivaluedMap<String,Object> headers = context.getHeaders();

headers.add("Content-Encoding", "gzip");

final OutputStream outputStream = context.getOutputStream();

context.setOutputStream(new GZIPOutputStream(outputStream));

context.proceed();

}

}

注意:

它实现了WriterInterceptor,这是一个写拦截器的消息体的接口,这个接口包装调用javax.ws.rs.ext.MessageBodyWriter.writeTo

供应商实现WriterInterceptor协议必须要么以编程方式注册进一个JAX-RS运行环境,要么必须用@Provider注解来注解在一个提供商扫描语句期间自动的被JAX-RS运行环境发现。

@Compress是绑定注解的名称,在接下来的段落中我们将更详细的讨论它

“拦截器从WriterInterceptorContext中获得一个输出流并且设置一个新的用原始的GZIP包装器包装的输出流。在所有的拦截器被执行以后,输出流最终设置WriterInterceptorContext将用于序列化实体。在上面的例子中,实体字节将被写到GZIPOutputStream中,这个类将压缩流数据,然后把他们写到原始输出流。原始流总是把数据写到wire中。当拦截器被用在服务器上时,原始输出流会把数据写到底层服务器容器的流中,然后发送响应给客户端。”

“重载方法aroundWriteTo()获取WriterInterceptorContextz作为参数。这个上下文包括请求头参数getters和setters,请求属性,实体,实体流和其它属性;当你压缩你的响应时,你应当设置'Content-Encoding'头位gzip”

1.2 压缩注解

过滤器和拦截器能被绑定名字。名称绑定是一种概念,这种概念就是允许告诉一个JAX-RS的运行时,一个只为特定资源方法的特定的过滤器或者拦截器将被执行。当一个过滤器或者拦截器只对一些特定的资源方法限制,那我们就认为它是名称绑定。过滤器和拦截器没有这样的限制就被称作global。在我们的例子中我们已经构建了@Compress注解:

Compress annotation

package org.codingpedia.demo.rest.interceptors;

import java.lang.annotation.Retention;

import java.lang.annotation.RetentionPolicy;

import javax.ws.rs.NameBinding;

//@Compress annotation is the name binding annotation

@NameBinding

@Retention(RetentionPolicy.RUNTIME)

public @interface Compress {}

而且用它来标记在资源上的方法,这个方法应该是被压缩的(eg:当GET-ing的时候,所有的博客用PodcastsResource)

@Compress annotation在资源方法上的使用

@Component

@Path("/podcasts")

public class PodcastsResource {

@Autowired

private PodcastService podcastService;

...........................

/*

* *********************************** READ ***********************************

*/

/**

* Returns all resources (podcasts) from the database

*

* @return

* @throws IOException

* @throws JsonMappingException

* @throws JsonGenerationException

* @throws AppException

*/

@GET

@Compress

@Proces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })

public List<Podcast> getPodcasts(

@QueryParam("orderByInsertionDate") String orderByInsertionDate,

@QueryParam("numberDaysToLookBack") Integer numberDaysToLookBack)

throws IOException,AppException {

List<Podcast> podcasts = podcastService.getPodcasts(

orderByInsertionDate, numberDaysToLookBack);

return podcasts;

}

...........................

}

2.测试

2.1SOAPui

好了,如果你正在用SOAPui测试,你能使用下面的请求违反PodcastsResource

Reqest:

请求例子:

GET http://localhost:8888/demo-rest-jersey-spring/podcasts/?orderByInsertionDate=DESC HTTP/1.1

Accept-Encoding: gzip,deflate

Accept: application/json, application/xml

Host: localhost:8888

Connection: Keep-Alive

User-Agent: Apache-HttpClient/4.1.1 (java 1.5)

Response:

被压缩的json响应,通过SOAPui自动的解压缩

HTTP/1.1 200 OK

Content-Type: application/json

Content-Encoding: gzip

Content-Length: 409

Server: Jetty(9.0.7.v20131107)

[

{

"id": 2,

"title": "Quarks & Co - zum Mitnehmen",

"linkOnPodcastpedia": "http://www.podcastpedia.org/quarks",

"feed": "http://podcast.wdr.de/quarks.xml",

"description": "Quarks & Co: Das Wissenschaftsmagazin",

"insertionDate": "2014-10-29T10:46:13.00+0100"

},

{

"id": 1,

"title": "- The Naked Scientists Podcast - Stripping Down Science",

"linkOnPodcastpedia": "http://www.podcastpedia.org/podcasts/792/-The-Naked-Scientists-Podcast-Stripping-Down-Science",

"feed": "feed_placeholder",

"description": "The Naked Scientists flagship science show brings you a lighthearted look at the latest scientific breakthroughs, interviews with the world top scientists, answers to your science questions and science experiments to try at home.",

"insertionDate": "2014-10-29T10:46:02.00+0100"

}

]

SOAPui接受Content-type:gzip头,我们在GZIPWriterIntercepter中添加了并且自动的解压了响应并且用人眼可读的方式展示出来。

好了,就这些了。你已经了解了Jersey如何让它直接压缩REST api响应了。

❿ 在java中,如何将含有多个文件的gzip解压

使用GZIPInputStream 和 GZIPOutputStream 。然后一个for循环。
如果你这样嫌麻烦的话,调用本地方法吧。

热点内容
winxp访问不了win7 发布:2024-05-05 23:05:23 浏览:733
算法牛 发布:2024-05-05 22:43:40 浏览:719
grublinux引导 发布:2024-05-05 22:37:56 浏览:215
unix高级编程第三版pdf 发布:2024-05-05 22:32:09 浏览:958
手机wap网站源码 发布:2024-05-05 22:27:44 浏览:259
python修改文件某一行 发布:2024-05-05 22:18:22 浏览:457
md5加密64 发布:2024-05-05 21:59:30 浏览:527
259pp页面访问升级 发布:2024-05-05 21:47:51 浏览:89
迅雷阻止上传 发布:2024-05-05 21:26:19 浏览:914
数据库运维题 发布:2024-05-05 21:21:47 浏览:962