Я пытаюсь заархивировать файлы с сервера в папку с помощью ZipOutputStream. После загрузки архива его нельзя открыть после двойного щелчка. Ошибка «Сбой (сжатая) папка недействительна». Но если я открою его из контекстного меню -> 7zip -> open file, он работает нормально. Что может быть причиной проблемы?Недопустимая папка с сжатой (zipped). Java
sourceFileName="./file.txt"'
sourceFile = new File(sourceFileName);
try {
// set the content type and the filename
responce.setContentType("application/zip");
response.addHeader("Content-Disposition", "attachment; filename=" + sourceFileName + ".zip");
responce.setContentLength((int) sourceFile.length());
// get a ZipOutputStream, so we can zip our files together
ZipOutputStream outZip = new ZipOutputStream((responce.getOutputStream());
// Add ZIP entry to output stream.
outZip.putNextEntry(new ZipEntry(sourceFile.getName()));
int length = 0;
byte[] bbuf = new byte[(int) sourceFile.length()];
DataInputStream in = new DataInputStream(new FileInputStream(sourceFile));
while ((in != null) && ((length = in.read(bbuf)) != -1)) {
outZip.write(bbuf, 0, length);
}
outZip.closeEntry();
in.close();
outZip.flush();
outZip.close();
Может быть, после того, как я разместил исходный код, вы помогли бы мне –
@ Ксения Шапошникова Welcome; Мне любопытно –
fileName в ContentDisposition заголовок должен быть просто «file.zip» без пути. Content-Length не следует указывать, поскольку вы не знаете (сжимается). Вы должны заменить outZip.close на outZip.finish; закрытие сервера JEE. –