我在本论坛上看到许多关于这个的解释和代码,但是代码都不能实现,因为是我太笨了吧,虽然作JAVA有段时间,但还是没有入门阿,我希望大家能给我个简单,明了的例子,像我的压缩代码一样:
import java.io.*;
import java.util.zip.*;public class SimpleZip { private static ZipOutputStream zos; public static void main(String[] args) {
String fileName = "d:/try";
try {
makeZip(fileName);
}
catch (Exception e) {
System.err.println(e);
}
} public static void makeZip(String fileName) throws IOException,
FileNotFoundException {
File file = new File(fileName);
zos = new ZipOutputStream(new FileOutputStream(file + ".zip"));
recurseFiles(file);
zos.close();
} private static void recurseFiles(File file) throws IOException,
FileNotFoundException {
if (file.isDirectory()) {
String[] fileNames = file.list();
if (fileNames != null) {
for (int i = 0; i < fileNames.length; i++) {
recurseFiles(new File(file, fileNames[i]));
}
}
}
else {
byte[] buf = new byte[1024];
int len;
ZipEntry zipEntry = new ZipEntry(file.toString());
FileInputStream fin = new FileInputStream(file);
BufferedInputStream in = new BufferedInputStream(fin);
zos.putNextEntry(zipEntry);
while ((len = in.read(buf)) >= 0) {
zos.write(buf, 0, len);
}
in.close();
zos.closeEntry();
}
}
}
附带:我是新手没有分给大家,希望大家谅解!!!!
import java.io.*;
import java.util.zip.*;public class SimpleZip { private static ZipOutputStream zos; public static void main(String[] args) {
String fileName = "d:/try";
try {
makeZip(fileName);
}
catch (Exception e) {
System.err.println(e);
}
} public static void makeZip(String fileName) throws IOException,
FileNotFoundException {
File file = new File(fileName);
zos = new ZipOutputStream(new FileOutputStream(file + ".zip"));
recurseFiles(file);
zos.close();
} private static void recurseFiles(File file) throws IOException,
FileNotFoundException {
if (file.isDirectory()) {
String[] fileNames = file.list();
if (fileNames != null) {
for (int i = 0; i < fileNames.length; i++) {
recurseFiles(new File(file, fileNames[i]));
}
}
}
else {
byte[] buf = new byte[1024];
int len;
ZipEntry zipEntry = new ZipEntry(file.toString());
FileInputStream fin = new FileInputStream(file);
BufferedInputStream in = new BufferedInputStream(fin);
zos.putNextEntry(zipEntry);
while ((len = in.read(buf)) >= 0) {
zos.write(buf, 0, len);
}
in.close();
zos.closeEntry();
}
}
}
附带:我是新手没有分给大家,希望大家谅解!!!!
楼主【yhxepoh】截止到2008-06-30 10:41:50的历史汇总数据(不包括此帖):
发帖数:1 发帖分:0
结贴数:0 结贴分:0
未结数:1 未结分:0
结贴率:0.00 % 结分率:-------------------
如何结贴请参考这里:http://topic.csdn.net/u/20080501/09/ef7ba1b3-6466-49f6-9d92-36fe6d471dd1.html
* 测试解压缩功能. 将d:\\download\\test.zip文件解压到d:\\temp\\zipout目录下.
* @param baseDir 目录
* @param file 要解压的ZIP文件
* @throws Exception
*/
public static void ExtractZip(String baseDir,String file) throws Exception {
// InputStream is=new BufferedInputStream(new FileInputStream());
//String baseDir = "D:\\workspace\\swing(js)\\";
ZipFile zfile = new ZipFile(baseDir + File.separator + file);
//System.out.println(zfile.getEntries());
Enumeration zList = zfile.getEntries();
ZipEntry ze = null;
byte[] buf = new byte[1024];
while (zList.hasMoreElements()) {
// 从ZipFile中得到一个ZipEntry
ze = (ZipEntry) zList.nextElement();
if (ze.isDirectory()) {
System.out.println("Dir: " + ze.getName() + " skipped..");
continue;
}
if(ze.getSize()<=0)
throw new Exception("文件长度为0");
System.out.println("Extracting: " + ze.getName() + "\t"
+ ze.getSize() + "\t" + ze.getCompressedSize()); // 以ZipEntry为参数得到一个InputStream,并写到OutputStream中
OutputStream os = new BufferedOutputStream(new FileOutputStream(
getRealFileName(baseDir, ze.getName())));
InputStream is = new BufferedInputStream(zfile.getInputStream(ze));
int readLen = 0;
while ((readLen = is.read(buf, 0, 1024)) != -1) {
os.write(buf, 0, readLen);
}
is.close();
os.close();
System.out.println("Extracted: " + ze.getName());
}
zfile.close();
}