javaSwing,怎样实现对文件的压缩与解压缩? 如题。使用的类应该是java.util.zip还是java.util.jar?具体怎么使用?请高手指点,最好给个例子,谢谢 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 压缩:ZipOutputStream zos=new ZipOutputStream(new FileOutputStream("d:/1.zip"));zos.putNextEntry(new ZipEntry("1.txt"));zos.write(...);//写入文件内容zos.closeEntry();zos.putNextEntry(new ZipEntry("doc/1.txt"));//另一个目录下的文件zos.write(...);//写入文件内容zos.closeEntry();zos.close();解压:用ZipInputStream,程序与上面类似如果用ZipFile也可以 import org.apache.tools.zip.ZipEntry;import org.apache.tools.zip.ZipFile;import org.apache.tools.zip.ZipOutputStream; public boolean zip(ZipOutputStream zos ,File f,String szBaseDir) throws IOException { File[] fList; int n=0; byte[] byBuf=new byte[m_nBufSize]; int nRead=0; ZipEntry ze; BufferedInputStream bis; if(!f.exists()) return false; if(f==null) f=new File("."); //若是目录,递归 if(f.isDirectory()){ fList=f.listFiles(); //遍历该目录下的各个文件 for(n=0;n<fList.length;++n){ if (zip(zos, fList[n], szBaseDir + f.getName() + File.separator) == false) return false; } //若是文件,写入压缩流 }else{ ze=new ZipEntry(szBaseDir+f.getName()); zos.putNextEntry(ze); bis=new BufferedInputStream(new FileInputStream(f)); while ((nRead = bis.read(byBuf)) >= 0) { zos.write(byBuf, 0, nRead); } bis.close(); zos.closeEntry(); } return true; } 关于显式声明类型形参 与 类型 通配符 的区别? aFile.delete()不管用不管用不管用 本人学习java时的几个作品,有源代码 高手来解决 swing 中如何改变按钮的形状 继承DefaultTableModel,实现了表的排序,但是由于该模型中的数据可以更新,最终导致表模型中的数据错乱,请高手指教 为什么内部类不能有static方法和static字段? java中怎么使用数据库中的image呀? ■□■□■ 不使用jb,用jdk怎么能把程序编译成exe????? 在线请教,请各位进来看看了。 有关JAVA中使用SQL语句后返回记录集的问题! 一个菜鸟的问题
ZipOutputStream zos=new ZipOutputStream(new FileOutputStream("d:/1.zip"));
zos.putNextEntry(new ZipEntry("1.txt"));
zos.write(...);//写入文件内容
zos.closeEntry();
zos.putNextEntry(new ZipEntry("doc/1.txt"));//另一个目录下的文件
zos.write(...);//写入文件内容
zos.closeEntry();
zos.close();解压:用ZipInputStream,程序与上面类似如果用ZipFile也可以
import org.apache.tools.zip.ZipFile;
import org.apache.tools.zip.ZipOutputStream; public boolean zip(ZipOutputStream zos ,File f,String szBaseDir) throws IOException {
File[] fList;
int n=0;
byte[] byBuf=new byte[m_nBufSize];
int nRead=0;
ZipEntry ze;
BufferedInputStream bis;
if(!f.exists())
return false;
if(f==null)
f=new File(".");
//若是目录,递归
if(f.isDirectory()){
fList=f.listFiles();
//遍历该目录下的各个文件
for(n=0;n<fList.length;++n){
if (zip(zos, fList[n], szBaseDir + f.getName() + File.separator) == false)
return false;
}
//若是文件,写入压缩流
}else{
ze=new ZipEntry(szBaseDir+f.getName());
zos.putNextEntry(ze);
bis=new BufferedInputStream(new FileInputStream(f));
while ((nRead = bis.read(byBuf)) >= 0) {
zos.write(byBuf, 0, nRead);
}
bis.close();
zos.closeEntry();
}
return true;
}