// Zip Test
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;public class SimpleZipTest { public static void main(String[] args) {
try {
File zipFile = new File("E:\\test.zip");
File beZippedFile = new File("E:\\forTest.dat");
ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(zipFile));
zipFile(beZippedFile, zos);
zos.flush();
zos.close();
System.out.println("Finish...");
} catch (Exception ex) {
ex.printStackTrace();
}
}
static void zipFile(File file, ZipOutputStream zos) throws FileNotFoundException, IOException {
byte[] readBuffer = new byte[2156];
int bytesIn = 0;
FileInputStream fis = new FileInputStream(file);
ZipEntry anEntry = new ZipEntry(file.getName());
anEntry.setSize(fis.available());
zos.putNextEntry(anEntry);
while ((bytesIn = fis.read(readBuffer)) != -1) {
zos.write(readBuffer, 0, bytesIn);
}
fis.close();
}
}
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;public class SimpleZipTest { public static void main(String[] args) {
try {
File zipFile = new File("E:\\test.zip");
File beZippedFile = new File("E:\\forTest.dat");
ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(zipFile));
zipFile(beZippedFile, zos);
zos.flush();
zos.close();
System.out.println("Finish...");
} catch (Exception ex) {
ex.printStackTrace();
}
}
static void zipFile(File file, ZipOutputStream zos) throws FileNotFoundException, IOException {
byte[] readBuffer = new byte[2156];
int bytesIn = 0;
FileInputStream fis = new FileInputStream(file);
ZipEntry anEntry = new ZipEntry(file.getName());
anEntry.setSize(fis.available());
zos.putNextEntry(anEntry);
while ((bytesIn = fis.read(readBuffer)) != -1) {
zos.write(readBuffer, 0, bytesIn);
}
fis.close();
}
}
解决方案 »
- 用java写一个socket 程序来抓取某个url的内容并且将url内容保存到本地
- 我在一个jdialog1中打开了另一个jdialog2,想实现单击jdialog2中的某个按钮可以刷新jdialog1中的表格。怎么实现呢
- 救救我啊java问题
- 国庆期间,特价计算机课程,实惠多多。
- 线程等待问题
- readObject()怎么不可以?
- 鼠标右键事件怎么写??
- 能否屏蔽JTree中的三击事件?
- 怎样使得JTextFeild只能输入ip地址
- 我的e盘上有resin 2.0.2 作为服务器。今天我今天在e盘又装了tomcat,结果两个都不能用了,为什么?
- java如何生成执行文件,只要用户点击图标就能执行
- 急!RTP协议怎么实现在internet上通信?
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;public class SimpleUnzipTest { public static void main(String[] args) {
String zipFile = new String("E:\\test.zip");
getExtractFileLength(zipFile);
}
static void getExtractFileLength(String zipFilePath) {
try {
FileInputStream inputstream = new FileInputStream(zipFilePath);
ZipInputStream zipinputstreamForLength = new ZipInputStream(inputstream);
ZipEntry zipentryForLength = null; while ((zipentryForLength = zipinputstreamForLength.getNextEntry()) != null) {
System.out.println(zipentryForLength.getSize());
}
zipinputstreamForLength.close();
inputstream.close();
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
http://community.csdn.net/Expert/TopicView1.asp?id=3222720
这个链接和本文看似无关啊To Gwyongcheng:
打包肯定成功了,没有抛出任何异常,用WinRAR和WinZip都可以正常打开