当前位置:首页 » 文件管理 » javagzip压缩文件夹

javagzip压缩文件夹

发布时间: 2023-05-21 19:18:27

A. 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;

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

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

C. 请问用java编写一个压缩程序,怎样解决压缩文件zip里的文件名乱码问题!

没做过压缩程序,JAVA里面的字符串使用的编码为unicode,ZIP文件里面用的应该是本地编码(中文操作系统用的是GB2312)。

你可以尝试着用类似这样的语句:String str = ( otherStr.getBytes("GB2312") );

祝好运。

D. 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) {
}

E. 在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("文件读取异常!");
}
}

F. java后台怎么接收一个gzip压缩流,并且解析接受参数

原则上,不需要在代码中处理zip只接收就可以。解析可以按HTTP协议自己解析,也可以使用WEB容器完成

G. java中zip压缩和gzip压缩的区别

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

H. java如何压缩成gz包

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

public class GZIPcompress {
public static void main(String[] args) {
try {
BufferedReader in =
new BufferedReader(
new FileReader(args[0]));
BufferedOutputStream out =
new BufferedOutputStream(
new GZIPOutputStream(
new FileOutputStream("test.gz")));
System.out.println("Writing file");
int c;
while((c = in.read()) != -1)
out.write(c);
in.close();
out.close();
System.out.println("Reading file");
BufferedReader in2 =
new BufferedReader(
new InputStreamReader(
new GZIPInputStream(
new FileInputStream("test.gz"))));
String s;
while((s = in2.readLine()) != null)
System.out.println(s);
} catch(Exception e) {
e.printStackTrace();
}
}
} ///:~

I. 把纯文本字符串用Gzip压缩再转换为Base64能有多少压缩率

其实具体多大压缩率要看源文件的内容,一般来说重复吵坦的单词越多,压缩率越高。

下面是把/usr/share/dict/words压缩的测升数桐试程序

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.zip.GZIPOutputStream;

import org.apache.commons.codec.binary.Base64;

public class GzipBase64Tests {

public static void main(String[] args) throws Exception {
File input = new File("/Users/matianyi/input.txt");
File output = new File("/Users/matianyi/output.txt");

if (!input.exists()) {
System.out.println("input file not exists!");
return;
}

if (output.exists()) {
output.delete();
}

ByteArrayOutputStream buffer = new ByteArrayOutputStream();
GZIPOutputStream gout = new GZIPOutputStream(buffer);

FileInputStream in = new FileInputStream(input);

long t1 = System.currentTimeMillis();
byte[] buf = new byte[1024];
int total=0;
int rd;
while ((rd = in.read(buf)) != -1) {
total += rd;
gout.write(buf,0, rd);
}

gout.close();
in.close();

byte[] result = buffer.toByteArray();

long t2 = System.currentTimeMillis();
String base64 = Base64.encodeBase64String(result);
long t3 = System.currentTimeMillis();

System.out.printf("raw %d -> gzip %d -> base64 %d, time1 %dms, time2 %dms"毕森, total, result.length, base64.length(), t2-t1, t3-t2);
}
}

输出为: raw 2493109 -> gzip 753932 -> base64 1005244, time1 225ms, time2 43ms

压缩了50%。

J. Java数据压缩格式程序设计方法

GZIP压缩格式简介在JDK API中 同样定义了多种类型用于创建和解除GZIP压缩格式数据文件的通用对象和方法 用于基于JDK编写GZIP压缩数据管理程序 GZIP压缩格式是在Sun Solaris操作系统中广泛采用的压缩数据格式 由于在数据压缩过程中可以采用多种类型的压缩算法 因此 压缩文件的压缩比很高 另外 在创建的压缩文件中 定义了用于表述时间和文件属主的时戳(Time Stamp) 可以使文件方便地在网络中传输和交换 GZIP压缩数据文件由一系列的数字构成 而各数字对应如下描述压缩文件信息的字段 ID 缺省值 用于标识GZIP压缩格式 ID 缺省值 用于标识GZIP压缩格式 CM 采用的压缩方法 其值为 ~ 是保留值 标识采用 deflate 压缩方法 FLG 用于标识各占用位的标志 MTIME 记录了最近修改时间 XFL 用于标识采用压缩算法的选项 OS 定义了操作系统类孝消告型 XLEN 定义了附加信息段的长度 M 压缩文件说明信息 CRC 记录了CRC 算法采用的循环冗余校验值 上述信息完整描述了GZIP压缩格式数据 当然 基于JDK开发的压缩数据管理程序 不需要明确知道上述压缩数据定义格式 只需要创建相应的管理对象并调用这些对象中定义的方法即可 JDK API中ZIP压缩格式支持对象GZIP压缩格式是在JDK API中定义支持的另外一种数据压缩格式 由上面桥仿介绍的GZIP格式数据压缩方法可知 GZIP压缩格式具有更大的压缩比 因此 在Unix操作系统中 这种类型的数据压缩形式的应用十分普及 与JDK API对ZIP压缩格式的支持不同 在JDK API中 只定义了GZIPInputStream和GZIPOutputStream两种类型的流(Stream)对象 用于在基于流的数据传输过程中实现数据压缩 这两个对象的继承定义结构如下所示 java lang Object|+ java io InputStream|+ java io FilterInputStream|+ java util zip InflaterInputStream|+ java util zip GZIPInputStream(java util zip GZIPOutputStream)巧明以采用GZIP格式进行数据输入处理GZIPInputStream对象为例 由上述对象的继承定义结构可以看出 该对象继承了InflaterInputStream流对象 需要说明的是 在ZIP压缩包中 定义了Inflater和Deflater两个对象 用于基于ZLIB压缩库实现多种格式的数据压缩和解压缩 因此 InflaterInputStream流对象的作用是采用ZLIB库作为数据压缩管理的引擎 而GZIPInputStream对象则进一步将流的数据加工进行细化 用于读取GZIP格式的压缩数据 同理 GZIPOutputStream对象用于创建GZIP格式的压缩数据文件 下面 将对两个对象的定义内容进行说明 ●GZIPInputStream对象定义结构 java util zip GZIPInputStream静态成员变量 protected CRC crc 用于说明采用的数据压缩算法为CRC protected boolean eos 说明输入流对象结束读取输入数据 构造方法 GZIPInputStream(InputStream in) 采用默认的缓冲区字节数创建输入流对象 GZIPInputStream(InputStream in int size) 创建由整数类型变量size指定缓冲区字节数的输入流对象 成员方法 该对象只定义了如下两个成员方法 void close() 关闭输入流对象 int read(byte[] buf int off int len) 读取输入流的数据到buf字节数组中 ●GZIPOutputStream对象定义结构 java util zip GZIPOutputStream静态成员变量 protected CRC crc 用于说明采用的数据压缩算法为CRC 构造方法 GZIPOutputStream(OutputStream out) 采用默认的缓冲区字节数创建输出流对象 GZIPOutputStream(OutputStream out int size) 创建由整数类型变量size指定缓冲区字节数的输出流对象 成员方法 void close() 关闭输出流对象 void finish() 结束数据输出 但不关闭输出流对象 void write(byte[] buf int off int len) 将字节数组buf中的内容压缩输出到输出流对象中 创建GZIP压缩格式文件实例经过前面对JDK API中创建GZIP压缩格式文件的相关对象的结构 成员方法定义形式的说明 读者一定会问如何应用这些对象和对象中定义的成员方法呢?请读者看下面的实例代码 //ZipDemo javaimport java io *; import java util zip *; public class GZIPDemo { public static void main(String[] args) { if (args length != ) { System out println("Usage:java GZIPDemo SourceFile DestnFile" + args length); System exit( ); } try { int number; //打开需压缩文件作为文件输入流 FileInputStream fin = new FileInputStream(args[ ]); //建立压缩文件输出流FileOutputStream fout=new FileOutputStream(args[ ]); //建立GZIP压缩输出流 GZIPOutputStream gzout=new GZIPOutputStream(fout); //设定读入缓冲区尺寸byte[] buf=new byte[ ]; while ((number = fin read(buf)) != ) gzout write(buf number); gzout close(); fout close(); fin close(); }catch(IOException e) { System out println(e); } } }上面的程序用于将命令行中指定的文件SourceFile进行压缩 创建GZIP格式的压缩文件DestnFile 在该程序的实现代码中 首先创建用于进行文件输入和输出的FileInputStream和FileOutputStream对象 并以FileOutputStream对象实例为参数创建GZIPOutputStream对象实例 从而为创建GZIP格式压缩文件建立数据流基础 在随后的代码中 利用FileInputStream对象中定义的read方法 从源文件中读取待压缩文件的内容 同时利用GZIPOutputStream对象中定义的write方法将压缩后的数据写出到输出文件中 从而实现数据文件的GZIP格式压缩处理 在Java中创建GZIP格式压缩文件的方法很简单 并且利用WinZip WinRAR等类型的压缩管理软件均能够打开创建的GZIP格式的压缩文件 那么 如何利用JDK API中定义的对象将被压缩的文件解压缩呢?请读者看下一节的内容 GZIP格式文件解压缩实例下面的程序用于将利用JDK API中定义对象的成员方法 将GZIP格式压缩文件进行解压缩 从而恢复压缩原始文件 //UnGZIPDemo javaimport java io *; import java util zip *; public class UnGZIPDemo { public static void main(String[] args) { if (args length != ) { System out println("Usage:java UnGZIPDemo GZIPFile DestnFile"); System exit( ); } try { int number;//建立GZIP压缩文件输入流 FileInputStream fin=new FileInputStream(args[ ]); //建立GZIP解压工作流 GZIPInputStream gzin=new GZIPInputStream(fin); //建立解压文件输出流 FileOutputStream fout=new FileOutputStream(args[ ]); //设定读入缓冲区尺寸byte[] buf=new byte[ ]; while ((nnumber=gzin read(buf buf length)) != ) fout write(buf nnumber); gzin close(); fout close(); fin close(); }catch(IOException e) { System out println(e); } } }在GZIP格式压缩文件解压缩程序代码中 仍然首先创建FileInputStream和FileOutputStream对象 并基于创建的FileInputStream对象创建GZIPInputStream对象 在随后的代码中 调用GZIPInputStream对象中定义的read方法 在从压缩文件中读取数据内容并进行解压缩处理后 将解除压缩后的数据内容利用文件输出流对象进行输出 从而实现数据文件的解压缩处理 小 lishixin/Article/program/Java/hx/201311/27034

热点内容
酒店配置什么灭火系统 发布:2025-02-09 08:06:37 浏览:773
java至尊 发布:2025-02-09 08:03:23 浏览:558
pythonwith 发布:2025-02-09 08:00:25 浏览:172
Ftp打开文件是只读模式 发布:2025-02-09 07:40:55 浏览:504
androidlistview点击事件 发布:2025-02-09 07:25:52 浏览:171
targz解压缩 发布:2025-02-09 06:59:19 浏览:311
wpsphp 发布:2025-02-09 06:58:41 浏览:962
视易锋云系统如何架设辅助服务器 发布:2025-02-09 06:47:08 浏览:770
mysql备份脚本shell 发布:2025-02-09 06:46:33 浏览:15
腾讯云服务器怎样调整分辨率 发布:2025-02-09 06:46:30 浏览:369