当前位置: 技术问答>java相关
听说java自带一个压缩的类,请问有知道这方面的没有?
来源: 互联网 发布时间:2015-02-17
本文导语: 听说java自带一个压缩的类,好象是jzip,请问有知道这方面的知识的吗? | Create a compressed (ZIP) file /* ** a simple ZIP tool ** ** ex. java Zip file.1 file.2 > file.zip ** */ import java.io.*; import jav...
听说java自带一个压缩的类,好象是jzip,请问有知道这方面的知识的吗?
|
Create a compressed (ZIP) file
/*
** a simple ZIP tool
**
** ex. java Zip file.1 file.2 > file.zip
**
*/
import java.io.*;
import java.util.zip.*;
class Zip {
public static void main(String args[]) throws IOException {
byte b[] = new byte[512];
ZipOutputStream zout = new ZipOutputStream(System.out);
for(int i = 0; i 0) {
long csize = e.getCompressedSize();
long ratio = ((size-csize)*100) / size;
err.println(" (deflated " + ratio + "%)");
}
else {
err.println(" (deflated 0%)");
}
}
else {
err.println(" (stored 0%)");
}
}
}
=================================
Expand the compressed (ZIP) file
/*
** a simple unZIP tool
**
** ex. java UnZip file.zip file1 to unzip file 1 from file.zip
** java UnZip file.zip to unzip file.zip
**
*/
import java.io.*;
import java.util.*;
import java.util.zip.*;
import java.text.*;
class UnZip {
public static void main(String args[]) throws IOException {
InputStream in = new BufferedInputStream(new FileInputStream(args[0]));
ZipInputStream zin = new ZipInputStream(in);
ZipEntry e;
while((e=zin.getNextEntry())!= null) {
if (args.length > 1) {
if (e.getName().equals(args[1])) {
unzip(zin, args[1]);
break;
}
}
unzip(zin, e.getName());
}
zin.close();
}
public static void unzip(ZipInputStream zin, String s) throws IOException {
System.out.println("unzipping " + s);
FileOutputStream out = new FileOutputStream(s);
byte [] b = new byte[512];
int len = 0;
while ( (len=zin.read(b))!= -1 ) {
out.write(b,0,len);
}
out.close();
}
}
==========================
Display compressed (ZIP) file content
/*
** a simple viewZIP tool
**
** ex. java ViewZip file.zip
**
*/
import java.io.*;
import java.util.*;
import java.util.zip.*;
import java.text.*;
class ViewZip {
public static void main(String args[]) throws IOException {
InputStream in = new BufferedInputStream(new FileInputStream(args[0]));
ZipInputStream zin = new ZipInputStream(in);
ZipEntry e;
System.err.println("Sizet Date Time Method Ratio Name");
System.err.println("----t ---- ---- ------ ----- ----");
while((e=zin.getNextEntry())!= null) {
zin.closeEntry();
print(e);
}
zin.close();
}
public static void print(ZipEntry e) {
PrintStream err = System.err;
err.print(e.getSize() + "t");
DateFormat df = new SimpleDateFormat ("yyyy.mm.dd hh:mm:ss");
Date d = new Date(e.getTime());
err.print(df.format(d) + " ");
if (e.getMethod() == ZipEntry.DEFLATED) {
err.print("deflated ");
long size = e.getSize();
if (size > 0) {
long csize = e.getCompressedSize();
long ratio = ((size-csize)*100) / size;
if (ratio
/*
** a simple ZIP tool
**
** ex. java Zip file.1 file.2 > file.zip
**
*/
import java.io.*;
import java.util.zip.*;
class Zip {
public static void main(String args[]) throws IOException {
byte b[] = new byte[512];
ZipOutputStream zout = new ZipOutputStream(System.out);
for(int i = 0; i 0) {
long csize = e.getCompressedSize();
long ratio = ((size-csize)*100) / size;
err.println(" (deflated " + ratio + "%)");
}
else {
err.println(" (deflated 0%)");
}
}
else {
err.println(" (stored 0%)");
}
}
}
=================================
Expand the compressed (ZIP) file
/*
** a simple unZIP tool
**
** ex. java UnZip file.zip file1 to unzip file 1 from file.zip
** java UnZip file.zip to unzip file.zip
**
*/
import java.io.*;
import java.util.*;
import java.util.zip.*;
import java.text.*;
class UnZip {
public static void main(String args[]) throws IOException {
InputStream in = new BufferedInputStream(new FileInputStream(args[0]));
ZipInputStream zin = new ZipInputStream(in);
ZipEntry e;
while((e=zin.getNextEntry())!= null) {
if (args.length > 1) {
if (e.getName().equals(args[1])) {
unzip(zin, args[1]);
break;
}
}
unzip(zin, e.getName());
}
zin.close();
}
public static void unzip(ZipInputStream zin, String s) throws IOException {
System.out.println("unzipping " + s);
FileOutputStream out = new FileOutputStream(s);
byte [] b = new byte[512];
int len = 0;
while ( (len=zin.read(b))!= -1 ) {
out.write(b,0,len);
}
out.close();
}
}
==========================
Display compressed (ZIP) file content
/*
** a simple viewZIP tool
**
** ex. java ViewZip file.zip
**
*/
import java.io.*;
import java.util.*;
import java.util.zip.*;
import java.text.*;
class ViewZip {
public static void main(String args[]) throws IOException {
InputStream in = new BufferedInputStream(new FileInputStream(args[0]));
ZipInputStream zin = new ZipInputStream(in);
ZipEntry e;
System.err.println("Sizet Date Time Method Ratio Name");
System.err.println("----t ---- ---- ------ ----- ----");
while((e=zin.getNextEntry())!= null) {
zin.closeEntry();
print(e);
}
zin.close();
}
public static void print(ZipEntry e) {
PrintStream err = System.err;
err.print(e.getSize() + "t");
DateFormat df = new SimpleDateFormat ("yyyy.mm.dd hh:mm:ss");
Date d = new Date(e.getTime());
err.print(df.format(d) + " ");
if (e.getMethod() == ZipEntry.DEFLATED) {
err.print("deflated ");
long size = e.getSize();
if (size > 0) {
long csize = e.getCompressedSize();
long ratio = ((size-csize)*100) / size;
if (ratio
您可能感兴趣的文章:
本站(WWW.)旨在分享和传播互联网科技相关的资讯和技术,将尽最大努力为读者提供更好的信息聚合和浏览方式。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。
站内导航:
特别声明:169IT网站部分信息来自互联网,如果侵犯您的权利,请及时告知,本站将立即删除!