ZipOutputStream을 사용하여 서버의 파일을 폴더로 압축하려고합니다. 아카이브 다운로드 후 두 번 클릭하여 열 수 없습니다. 오류 "압축 된 (압축 된) 폴더가 잘못되었습니다."라는 오류가 발생합니다. 하지만 컨텍스트 메뉴에서 열면 -> 7zip -> 파일 열기가 정상적으로 작동합니다. 문제의 원인은 무엇일까요?압축 된 (압축 된) 폴더가 유효하지 않습니다. 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();
소스 코드를 게시하면 도움이됩니다. –
@ Ксения Шапошникова 환영합니다. 궁금한데 –
ContentDisposition 헤더의 fileName은 경로없이 "file.zip"이어야합니다. 당신이 알지 못하는 (압축되어있다) Content-Length는 주어져서는 안된다. outZip.finose를 outZip.finish로 바꿔야합니다. closing은 JEE 서버를 수행합니다. –