Bu yüzden bir zip dosyası oluşturdum ve "\" ile biten bir zip girişi oluşturarak zip dosyasında yeni bir alt klasör oluşturdum. Alt klasöre nasıl yazarım?Zip'de alt dizinlere yazmak ister misiniz? Java.util.zip'i kullanma. ZipEntry.putNextEntry()
Sorunum, bir for döngüsünde ZipOutputStream sayfamda bir putnextEntry çağrısı var, böylece klasör oluşturulduktan sonra farklı zip dosyalarının yazıldığı bir for döngüsüne atlayın. Ancak, zip içindeki alt dizinle aynı düzeyde yazılırlar.
Ne olduğunu düşünüyorum çünkü putNextEntry'yi ilk gerçek zip (dir) değil, alt klasörü kapatıp fermuarın köküne yazıyor. Herhangi bir fikir?
private int endprocess() {
try {
zipFolder(ripPath, zipOutputPath, "rips");
//zipFolder(destPDFfiles, zipOutputPath, "pdfs");
this.returnCode = 0;
//log.debug ("Accumulator count: " + acount);
log.debug("Equivest count: " + ecount);
//log.debug ("Assoc count: " + scount);
processEndOfEnvelope();
} catch (Exception reportException) {
log.logError("Caught exception in creating.");
reportException.printStackTrace();
this.returnCode = 15;
}
return (this.returnCode);
}
public static void zipFolder(String srcFolder, String dest, String outputFolder){
try{
ZipOutputStream zos = null;
FileOutputStream fos = null;
fos = new FileOutputStream(dest + "\\newzip.zip");
zos = new ZipOutputStream(fos);
addFolderToZip(srcFolder, zos, outputFolder);
zos.flush();
zos.close();
}catch(IOException e){
log.logError("**********************");
log.logError("IO Exception occurred");
log.logError(e.getMessage());
e.printStackTrace();
}
}
private static void addFileToZip(String srcFile, ZipOutputStream zos, String outputFolder){
try {
File folder = new File(srcFile);
if (folder.isDirectory()) {
addFolderToZip(srcFile, zos, outputFolder);
} else {
byte[] buffer = new byte[1024];
int length;
FileInputStream fis = new FileInputStream(srcFile);
ZipEntry ze = new ZipEntry("C:\\AWDAAV\\zip\\newzip.zip\\" + outputFolder + "\\" + folder.getName());
zos.putNextEntry(ze);
while ((length = fis.read(buffer)) > 0) {
zos.write(buffer, 0, length);
}
}
}catch(Exception e){
log.logError("**********************");
log.logError("Exception occurred");
log.logError(e.getMessage());
e.printStackTrace();
}
}
private static void addFolderToZip(String srcFolder, ZipOutputStream zos, String outputFolder){
try{
File folder = new File(srcFolder);
zos.putNextEntry(new ZipEntry(outputFolder + "\\"));
for(String fileName : folder.list()){
addFileToZip(srcFolder + "\\" + fileName, zos, outputFolder);
}
}catch(Exception e){
log.logError("**********************");
log.logError("Exception occurred");
log.logError(e.getMessage());
e.printStackTrace();
}
}
Eklendi kodu. – hfrog713