主要思想:
把原来的zip中的内容读入temp文件再把要加入的文件加入其中,然后将原来的文件aaa.zip删除,最后将这个temp文件改名为你的文件名aaa.zip。下面是一个片断,仅供参考! /** 加入已存在的文件的问题已解决
* add file list into the zip file
* @param []szFilePath -the full path of new file
* @return
*/
public boolean addFile(String []szFilePath){//增加一个文件到zip中
String entryname = "" ;
String fullname = ""; BufferedInputStream bin = null;
BufferedOutputStream bout = null;
ZipOutputStream zout = null;
ZipInputStream zin = null;
File tmpzip = null;
Vector vector=new Vector();//control a file has existed
try{
tmpzip = File.createTempFile("zip",".tmp",new File(".\\"));
zin = new ZipInputStream(new FileInputStream(this.m_szFilePath));
zout = new ZipOutputStream(new FileOutputStream(tmpzip));
ZipEntry ze;
int len = 0;
byte[] b = new byte[10240];
while((ze=zin.getNextEntry())!=null){
zout.putNextEntry(new ZipEntry(ze.getName()));
while((len=zin.read(b))!=-1){
zout.write(b,0,len);
}
vector.addElement(ze.getName());
zout.closeEntry();
zin.closeEntry();
}
if(szFilePath==null)
return false;
int filecount=szFilePath.length;
while(filecount-->0){
File f=new File(szFilePath[filecount]);//get the String path
if(!f.exists())//no exist!
return false;
fullname=f.getAbsolutePath() ;//file
bin = new BufferedInputStream(new FileInputStream(fullname));
ze = new ZipEntry(new File(fullname).getName());
if(!vector.contains(ze.getName())){//control a file has existed
this.m_vEntityName.addElement(ze.getName()) ;
zout.putNextEntry(ze);
while((len = bin.read(b))!=-1){
zout.write(b,0,len);
}
}
zout.closeEntry();
bin.close();
} zout.close();
zin.close();
File tmpfile = new File(this.m_szFilePath);
String tmpname = tmpfile.getPath();
tmpfile.delete();
tmpzip.renameTo(new File(tmpname));
return true;
}
catch(IOException e){
System.err.println("I/O Exception addFile,maybe the file exists");
return false;
}
}
把原来的zip中的内容读入temp文件再把要加入的文件加入其中,然后将原来的文件aaa.zip删除,最后将这个temp文件改名为你的文件名aaa.zip。下面是一个片断,仅供参考! /** 加入已存在的文件的问题已解决
* add file list into the zip file
* @param []szFilePath -the full path of new file
* @return
*/
public boolean addFile(String []szFilePath){//增加一个文件到zip中
String entryname = "" ;
String fullname = ""; BufferedInputStream bin = null;
BufferedOutputStream bout = null;
ZipOutputStream zout = null;
ZipInputStream zin = null;
File tmpzip = null;
Vector vector=new Vector();//control a file has existed
try{
tmpzip = File.createTempFile("zip",".tmp",new File(".\\"));
zin = new ZipInputStream(new FileInputStream(this.m_szFilePath));
zout = new ZipOutputStream(new FileOutputStream(tmpzip));
ZipEntry ze;
int len = 0;
byte[] b = new byte[10240];
while((ze=zin.getNextEntry())!=null){
zout.putNextEntry(new ZipEntry(ze.getName()));
while((len=zin.read(b))!=-1){
zout.write(b,0,len);
}
vector.addElement(ze.getName());
zout.closeEntry();
zin.closeEntry();
}
if(szFilePath==null)
return false;
int filecount=szFilePath.length;
while(filecount-->0){
File f=new File(szFilePath[filecount]);//get the String path
if(!f.exists())//no exist!
return false;
fullname=f.getAbsolutePath() ;//file
bin = new BufferedInputStream(new FileInputStream(fullname));
ze = new ZipEntry(new File(fullname).getName());
if(!vector.contains(ze.getName())){//control a file has existed
this.m_vEntityName.addElement(ze.getName()) ;
zout.putNextEntry(ze);
while((len = bin.read(b))!=-1){
zout.write(b,0,len);
}
}
zout.closeEntry();
bin.close();
} zout.close();
zin.close();
File tmpfile = new File(this.m_szFilePath);
String tmpname = tmpfile.getPath();
tmpfile.delete();
tmpzip.renameTo(new File(tmpname));
return true;
}
catch(IOException e){
System.err.println("I/O Exception addFile,maybe the file exists");
return false;
}
}
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货