import java.util.zip.*;
import java.io.*; public class Zipper
{
private String Name;
private ZipOutputStream zip;
public String getName()
{
return Name;
}
public void setName(String newName)
{
Name=newName;
}
//构造一个ZipOutputStream输出流利用输出流zip
//添加方法完成的初始化
public int init()
{
//建立并且初始化一个输入流
try
{
//ZIP输出流的建立
zip=new ZipOutputStream(new FileOutputStream(Name));
//Sets the ZIP file comment.
zip.setComment("Zip file created by java");
//设置entry压缩方法,缺省值为DEFLATED
zip.setMethod(ZipOutputStream.DEFLATED);
//设置entry压缩水平,缺省值为DEFAULT_COMPRESSION
zip.setLevel(compressionLevel);
}
catch(Exception e)
{
System.out.println(e);
}
return 0;
}
public int close()
//关闭输入流,清空缓冲区
{
try
{
zip.close();
}
catch(Exception e)
{
System.out.println(e);
return 1;
}
return 0;
}
public int addFile(String fileName)
{
//编写增加压缩文件的函数
int entryNum;
try
{
File file =new File (fileName);
FileInputStream in =new FileInputStream(file);
byte[] bytes=new byte[in.available()];
// in.available()方法返回的是输入流中可以正擦读取的字节数值,
// 可以是读取的字节的数目,可以避免阻塞的问题,因为我们
// 只是从流中读取能够读取的字节的序列
in.read(bytes);
in.close();
//建立并且初始化zipEntry
//getName()返回条目的名称
ZipEntry entry =new ZipEntry(file.getName());
//设置条目修改的时间
entry.setTime(file.lastModified());
//向zip文件中写入数据
zip.putNextEntry(entry);
//如果当前的entry存在且处于激活状态时,关闭它,在zip文件中写入新的entry
//并将数据流定位于entry数据项的起始位置,压缩方法为setMethod指定的方法。
zip.write(bytes);
zip.closeEntry();
//关闭当前的zip entry,并将数据流定位于下一个entry的起始位置。
}
catch(Exception e)
{
System.out.println(e);
}
entryNum++;
return 0;
}
public static void main(String[] args)
{
int compressionLevel=Deflater.DEFAULT_COMPRESSION;
int argc=args.length;
//argc是程序运行参数的长度
int argn;
//argn用于解释命令行参数
//解释命令行参数
for(argn=0;(argn<args.length)&&args[argn].charAt(0)=='-';++argn)
//(argn<args.length)&&args[argn].charAt(0)=='-';
//表示没有循环完所有的参数的数量并且参数的是以"-"为开始的
//备注:Zipper是通过Zipper -?来使用参数的 -?是参数。args[]就是传进来的参数。
{
if (args[argn].equals("-1"))
//如果参数为-1
{
++argn;
compressionLevel=Integer.parseInt(args[argn]);
}
else
{
System.out.println("usage:"
+" Ziper[-1 Level] zipfile files");
return;
}
}
if (argc-argn<1)
//已经检查参数了
{
System.out.println("usage:Zipper [-1 Level] zipfile files");
return;
}
else try
{
//余下的第一个参数是生成zip文件的名称
Zipper zip =new Zipper(args[argn++]);
zip.compressionLevel=compressionLevel;
zip.init();
//其余的参数是需要压缩的文件名称
for (;argn<args.length;++argn)
{
//将一个文件读入内存
zip.addFile(args[argn]);
zip.close();
}
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
import java.io.*; public class Zipper
{
private String Name;
private ZipOutputStream zip;
public String getName()
{
return Name;
}
public void setName(String newName)
{
Name=newName;
}
//构造一个ZipOutputStream输出流利用输出流zip
//添加方法完成的初始化
public int init()
{
//建立并且初始化一个输入流
try
{
//ZIP输出流的建立
zip=new ZipOutputStream(new FileOutputStream(Name));
//Sets the ZIP file comment.
zip.setComment("Zip file created by java");
//设置entry压缩方法,缺省值为DEFLATED
zip.setMethod(ZipOutputStream.DEFLATED);
//设置entry压缩水平,缺省值为DEFAULT_COMPRESSION
zip.setLevel(compressionLevel);
}
catch(Exception e)
{
System.out.println(e);
}
return 0;
}
public int close()
//关闭输入流,清空缓冲区
{
try
{
zip.close();
}
catch(Exception e)
{
System.out.println(e);
return 1;
}
return 0;
}
public int addFile(String fileName)
{
//编写增加压缩文件的函数
int entryNum;
try
{
File file =new File (fileName);
FileInputStream in =new FileInputStream(file);
byte[] bytes=new byte[in.available()];
// in.available()方法返回的是输入流中可以正擦读取的字节数值,
// 可以是读取的字节的数目,可以避免阻塞的问题,因为我们
// 只是从流中读取能够读取的字节的序列
in.read(bytes);
in.close();
//建立并且初始化zipEntry
//getName()返回条目的名称
ZipEntry entry =new ZipEntry(file.getName());
//设置条目修改的时间
entry.setTime(file.lastModified());
//向zip文件中写入数据
zip.putNextEntry(entry);
//如果当前的entry存在且处于激活状态时,关闭它,在zip文件中写入新的entry
//并将数据流定位于entry数据项的起始位置,压缩方法为setMethod指定的方法。
zip.write(bytes);
zip.closeEntry();
//关闭当前的zip entry,并将数据流定位于下一个entry的起始位置。
}
catch(Exception e)
{
System.out.println(e);
}
entryNum++;
return 0;
}
public static void main(String[] args)
{
int compressionLevel=Deflater.DEFAULT_COMPRESSION;
int argc=args.length;
//argc是程序运行参数的长度
int argn;
//argn用于解释命令行参数
//解释命令行参数
for(argn=0;(argn<args.length)&&args[argn].charAt(0)=='-';++argn)
//(argn<args.length)&&args[argn].charAt(0)=='-';
//表示没有循环完所有的参数的数量并且参数的是以"-"为开始的
//备注:Zipper是通过Zipper -?来使用参数的 -?是参数。args[]就是传进来的参数。
{
if (args[argn].equals("-1"))
//如果参数为-1
{
++argn;
compressionLevel=Integer.parseInt(args[argn]);
}
else
{
System.out.println("usage:"
+" Ziper[-1 Level] zipfile files");
return;
}
}
if (argc-argn<1)
//已经检查参数了
{
System.out.println("usage:Zipper [-1 Level] zipfile files");
return;
}
else try
{
//余下的第一个参数是生成zip文件的名称
Zipper zip =new Zipper(args[argn++]);
zip.compressionLevel=compressionLevel;
zip.init();
//其余的参数是需要压缩的文件名称
for (;argn<args.length;++argn)
{
//将一个文件读入内存
zip.addFile(args[argn]);
zip.close();
}
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
能够传值给private String Name;吗?
为什么init()不用传值?
还有Zipper zip =new Zipper(args[argn++]);
产生的对象zip是在整个类是可见的吗?
E:\study\java\Zipper.java:33: 找不到符号
符号: 变量 compressionLevel
位置: 类 Zipper
zip.setLevel(compressionLevel);
^
E:\study\java\Zipper.java:129: 找不到符号
符号: 构造函数 Zipper(java.lang.String)
位置: 类 Zipper
Zipper zip =new Zipper(args[argn++]);
^
E:\study\java\Zipper.java:130: 找不到符号
符号: 变量 compressionLevel
位置: 类 Zipper
zip.compressionLevel=compressionLevel;
^
3 错误处理已完成。
你的类也没有实现这个方法,Zipper zip =new Zipper(args[argn++]);
你的类里就没有定义这个构造方法,当然报错了zip.compressionLevel=compressionLevel;
这个zip.compressionLevel你已经定义为private 私有成员变量,只有在类的内类可以访问
不可以通过类名只接访问,除非定义成static静态类型变量