建议看 java.util.zip 下的相关文件,其实没什么太大区别的,给段代码,用以压缩文件 /**
* 压缩指定的文件列表到一个Zip包
* @param target 目标Zip文件
* @param sources 要压缩的源文件列表
* @param basePath 相对压缩的路径
* @throws IOException
*/
public void compress(File target,File[] sources,String basePath) throws IOException{
if(sources==null||sources.length==0)
return;
byte[] buffer = new byte[4096];
int length = 0;
FileOutputStream fout = new FileOutputStream(target);
ZipOutputStream zip = new ZipOutputStream(fout);
for(int i=0;i<sources.length;i++){
FileInputStream fin = new FileInputStream(sources[i]);
String path = sources[i].getAbsolutePath();
if(path.startsWith(basePath))
path = path.substring(basePath.length());
if(path.charAt(0)==File.separatorChar)
path = path.substring(1);
ZipEntry entry = new ZipEntry(path);
zip.putNextEntry(entry);
while((length=fin.read(buffer))!=-1){
zip.write(buffer,0,length);
}
fin.close();
zip.closeEntry();
}
zip.close();
}
* 压缩指定的文件列表到一个Zip包
* @param target 目标Zip文件
* @param sources 要压缩的源文件列表
* @param basePath 相对压缩的路径
* @throws IOException
*/
public void compress(File target,File[] sources,String basePath) throws IOException{
if(sources==null||sources.length==0)
return;
byte[] buffer = new byte[4096];
int length = 0;
FileOutputStream fout = new FileOutputStream(target);
ZipOutputStream zip = new ZipOutputStream(fout);
for(int i=0;i<sources.length;i++){
FileInputStream fin = new FileInputStream(sources[i]);
String path = sources[i].getAbsolutePath();
if(path.startsWith(basePath))
path = path.substring(basePath.length());
if(path.charAt(0)==File.separatorChar)
path = path.substring(1);
ZipEntry entry = new ZipEntry(path);
zip.putNextEntry(entry);
while((length=fin.read(buffer))!=-1){
zip.write(buffer,0,length);
}
fin.close();
zip.closeEntry();
}
zip.close();
}
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货