У меня есть функция, которая создает Zip-файл из списка файлов. Можно ли вернуть Zip-файл без его сохранения на диск? Мне нужен файл, так как я должен использовать zip-файл в качестве параметра для другой функции. Я не уверен, что ByteStream подойдет мне.
public File compressFileList(List<File> fileList,String fileName) {
FileOutputStream fileOutputStream=null;
ZipOutputStream zipOutputStream=null;
FileInputStream fileInputStream=null;
String compressedFileName=fileName +".zip";
if(fileList.isEmpty())
return null;
try
{
fileOutputStream = new FileOutputStream(compressedFileName);
zipOutputStream = new ZipOutputStream(new BufferedOutputStream(fileOutputStream));
for (File file: fileList) {
fileInputStream = new FileInputStream(file);
ZipEntry zipEntry = new ZipEntry(file.getName());
zipOutputStream.putNextEntry(zipEntry);
byte[] tmp = new byte[4*1024];
int size = 0;
while((size = fileInputStream.read(tmp)) != -1){
zipOutputStream.write(tmp, 0, size);
}
zipOutputStream.flush();
fileInputStream.close();
}
zipOutputStream.close();
return compressedFile; //This is what I am missing
}
catch (FileNotFoundException e)
{
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
EDIT: добавление варианта использования
Идея состоит в том, чтобы создать ZIP-файл и использовать метод CreateClassifierOptions службы визуального распознавания Watson.
classifierOptions = new CreateClassifierOptions.Builder()
.classifierName("Santa")
.addClass("Santa", new File("C:\\app\\GitRepo\\images\\beagle.zip"))
.negativeExamples(new File("C:\\app\\GitRepo\\images\\nosport.zip"))
.build();
Построитель принимает zip-файл в качестве параметра.
Понимание
Основываясь на объяснении Александра Дюприе, я думаю, что лучше хранить файл в каком-то месте на жестком диске.
Inputstream
? - person Lino   schedule 13.11.2017