把你的代码放上来,ZIP的,gz文件在PC上不流行
解决方案 »
- java wait(long num)的问题
- jdom怎么设置字符集的编码,解析xml文件出现乱码
- Java 是否可以使用问号表达式?(c=a<b? a : b;)
- 关于javascript的问题,请高手指教!
- 如何在java的client中使用HttpURLConnection类实现POST类型的传输将文本框中的大文本在servlet中接收到
- 窗体的显示
- static和final修饰数据成员的问题!
- 请问大家开发JAVA程序时用什么工具?
- 如何安装Jbuilder 5.0 ?
- RMI server的起动。(gxg3)
- 如果在文件夹下面包含有文件,如何删除该文件夹
- this、super问题急求解
FileOutputStream( zipFile );
CheckedOutputStream csum =
new CheckedOutputStream(
fos, new Adler32()
);
ZipOutputStream out =
new ZipOutputStream(
new BufferedOutputStream( csum )
);
BufferedReader in =
new BufferedReader(
new FileReader( srcfile )
);
ZipEntry entry = new ZipEntry( srcfile );
out.putNextEntry( entry );
int c = -1;
while( ( c = in.read() ) != -1 ) {
out.write( c );
}
in.close();
out.close();
其中,zipFile和srcfile为类似于"e:\x\xx\xxx\xxx.xx"这样的全路径
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 < args.length; i ++) {
InputStream in = new FileInputStream(args[i]);
ZipEntry e = new ZipEntry(args[i].replace(File.separatorChar,'/'));
zout.putNextEntry(e);
int len=0;
while((len=in.read(b)) != -1) {
zout.write(b,0,len);
}
zout.closeEntry();
print(e);
}
zout.close();
}
public static void print(ZipEntry e){
PrintStream err = System.err;
err.print("added " + e.getName());
if (e.getMethod() == ZipEntry.DEFLATED) {
long size = e.getSize();
if (size > 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%)");
}
}
}
压缩文件反而比原文件更大是不可能的。打开zip文件,一个文件的原始大小是ZipEntry.getSize();压缩后的大小是ZipEntry.getComproessedSize().