これは最終的な方法ですが、これは私が実際にstackoverflowトピックから得たpastelistnedメソッドと同じです@Qweは前に投稿しましたが、パス変数を追加してzip内のフォルダにファイルを追加できるようにしました
さて今、私は別のフォルダ内に、私はこの
private void addFilesToZip(File source, File[] files, String path){
try{
File tmpZip = File.createTempFile(source.getName(), null);
tmpZip.delete();
if(!source.renameTo(tmpZip)){
throw new Exception("Could not make temp file (" + source.getName() + ")");
}
byte[] buffer = new byte[4096];
ZipInputStream zin = new ZipInputStream(new FileInputStream(tmpZip));
ZipOutputStream out = new ZipOutputStream(new FileOutputStream(source));
for(int i = 0; i < files.length; i++){
InputStream in = new FileInputStream(files[i]);
out.putNextEntry(new ZipEntry(path + files[i].getName()));
for(int read = in.read(buffer); read > -1; read = in.read(buffer)){
out.write(buffer, 0, read);
}
out.closeEntry();
in.close();
}
for(ZipEntry ze = zin.getNextEntry(); ze != null; ze = zin.getNextEntry()){
if(!zipEntryMatch(ze.getName(), files, path)){
out.putNextEntry(ze);
for(int read = zin.read(buffer); read > -1; read = zin.read(buffer)){
out.write(buffer, 0, read);
}
out.closeEntry();
}
}
out.close();
tmpZip.delete();
}catch(Exception e){
e.printStackTrace();
}
}
private boolean zipEntryMatch(String zeName, File[] files, String path){
for(int i = 0; i < files.length; i++){
if((path + files[i].getName()).equals(zeName)){
return true;
}
}
return false;
}
のような問題のためのおかげで、私のセットアップを使用していることをするだろうされたフォルダにファイルを追加したい上記の私の例ではそれを使用する方法リンクは、その方法を少し改良して、ルートになかったファイルを追加できるようになりました。これは他の誰かアウトだけでなく
EDIT 、それがZIPに追加することができないだけではなく、またのzip内のファイルを更新することができるように、私はこの方法でもう少し仕事をするのに役立ちます願っています:)幸せなキャンピングカー
ことは、それがZIPエントリ
HTTPに記載されていない方法だとして、あなたが/とパス(最後の変数)を開始しません。この
のようなメソッドを使用します:// stackoverflowの。com/questions/2223434/appending-files-to-a-zip-file-with-java –