I want to create a ZipDirectory with my all files but when I create it, it's an empty directory. There are no files. I verify the length of my files and there are a length so I don't understand why my ZipDirectory is empty.
How can I solve my problem?
@Override
public byte[] zipResourceDirectory(String path, String directory, ApiParam api) throws IOException {
//creating byteArray stream, make it bufforable and passing this buffor to ZipOutputStream
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(byteArrayOutputStream);
ZipOutputStream zipOutputStream = new ZipOutputStream(bufferedOutputStream);
String prefix = directory;
zipDirectory(api, zipOutputStream, path, prefix, directory);
zipOutputStream.finish();
zipOutputStream.flush();
return byteArrayOutputStream.toByteArray();
}
private void zipDirectory(ApiParam api, ZipOutputStream zipOutputStream, String path, String prefix, String directory) throws IOException {
try {
Assert.notNull(api, "api is empty");
Assert.notNull(zipOutputStream, "zipOutputStream is empty");
Assert.notNull(path, "prefix is empty");
Assert.notNull(directory, "directory is empty");
ClassLoader classLoader = getClass().getClassLoader();
File folder = new File(classLoader.getResource(path).getFile());
System.out.println(folder.getAbsolutePath());
Assert.notNull(directory, "folder is empty");
File[] listOfFiles = folder.listFiles();
Assert.notNull(listOfFiles, "listOfFiles is empty");
for (int i = 0; i < listOfFiles.length; i++) {
if (listOfFiles[i].isFile()) {
addFileToZip(api, zipOutputStream, prefix ,listOfFiles[i], directory);
} else if (listOfFiles[i].isDirectory()) {
addDrirectoryToZip(api, zipOutputStream, path, prefix, listOfFiles[i]);
}
}
}
catch (Exception e) {
System.out.println(e);
}
}
private void addDrirectoryToZip(ApiParam api, ZipOutputStream zipOutputStream, String path, String prefix, File file) throws IOException {
try {
Assert.notNull(api, "api is empty");
Assert.notNull(zipOutputStream, "zipOutputStream is empty");
Assert.notNull(path, "prefix is empty");
Assert.notNull(file, "file is empty");
zipOutputStream.putNextEntry(new ZipEntry(prefix + "/" + file.getName() + "/"));
zipOutputStream.closeEntry();
System.out.println("Directory " + file.getName());
zipDirectory(api, zipOutputStream, path + "/" + file.getName(), prefix + "/" + file.getName(), file.getName());
}
catch (Exception e) {
System.out.println(e);
}
}
private void addFileToZip(ApiParam api, ZipOutputStream zipOutputStream, String prefix, File file, String directory) throws IOException {
try {
Assert.notNull(api, "api is empty");
Assert.notNull(zipOutputStream, "zipOutputStream is empty");
Assert.notNull(file, "file is empty");
Assert.notNull(directory, "directory is empty");
System.out.println("File " + file.getName());
if (file.getName().equals("template_v1.xml")) {
zipOutputStream.putNextEntry(new ZipEntry(prefix + "/"+api.getProxyName()+"_"+api.getVersion()+".xml"));
}
else {
zipOutputStream.putNextEntry(new ZipEntry(prefix + "/" + file.getName()));
}
System.out.println("File2" + file.getName());
FileInputStream fileInputStream = new FileInputStream(file);
StringBuilder builder = new StringBuilder();
int ch;
while ((ch = fileInputStream.read()) != -1){
builder.append((char) ch);
}
String outputString = fileBuilderService.build(directory, file.getName(), builder.toString(), api);
InputStream inputStream = new ByteArrayInputStream(outputString.getBytes());
int length;
byte[] bytes = new byte[1024];
while ((length = inputStream.read(bytes)) > 0) {
zipOutputStream.write(bytes, 0, length);
}
inputStream.close();
fileInputStream.close();
zipOutputStream.closeEntry();
}
catch (Exception e) {
System.out.println(e);
}
}
Thanks a lot.