2017-03-23 12 views
3

문자열로 변환 된 XML 목록을 압축하려고 하나의 zip 파일에 저장하고 POST 본체로 되돌아갑니다. 그러나 파일을 저장할 때마다 "아카이브가 알 수없는 형식이거나 손상되었습니다."라는 오류 메시지가 나타납니다.압축 (Zip) ZipOutPutStream을 사용하여 파일 목록 Java

protected ByteArrayOutputStream zip(Map<String, String> mapConvertedXml) { 
    ByteArrayOutputStream baos = new ByteArrayOutputStream(); 
    ZipOutputStream zos = new ZipOutputStream(baos); 
    try { 
     for(Map.Entry<String, String> current : mapConvertedXml.entrySet()){ 

      ZipEntry entry = new ZipEntry(current.getKey() + ".xml"); 
      entry.setSize(current.getValue().length()); 
      zos.putNextEntry(entry); 
      zos.write(current.getValue().getBytes()); 
      zos.flush(); 
     } 

     zos.close(); 

    } catch (IOException ioe) { 
     ioe.printStackTrace(); 
    } 
    return baos; 
} 

아무도 도와 줄 수 있습니까?

+0

네트워크로 전송하기 전에 zip 파일을 검사 해 보셨습니까? – nandsito

+1

사용중인 charset은 무엇입니까? String의 길이가 바이트 배열의 길이와 항상 일치하지는 않기 때문입니다. – Boschi

+0

@nandsito 아니오 ZipOutputStream에 대한 함수가 있습니까? –

답변

0

zip 파일을 테스트하려면 임시로 파일 시스템에 저장하고 수동으로여십시오.

FileOutputStream fos = new FileOutputStream(pathToZipFile); 
ZipOutputStream zos = new ZipOutputStream(fos); 

그런 다음이를 사용하여 Rest Server를 구성하고 파일을 반환 할 수 있습니다.

public static Response getFileLast(@Context UriInfo ui, @Context HttpHeaders hh) { 
    Response response = null; 
    byte[] sucess = null; 
    ByteArrayOutputStream baos = getYourFile(); 
    sucess = baos.toByteArray(); 
    if(sucess!=null) { 
     response = Response.ok(sucess, MediaType.APPLICATION_OCTET_STREAM).header("content-disposition","attachment; filename = YourFileName").build(); 
    } else { 
     response = Response.status(Status.NOT_FOUND).type(MediaType.APPLICATION_JSON).entity(new Error("Error downloading file.")).build(); 
    } 

    return response; 
} 

예를 들어 Advanced Rest Client을 테스트 할 수 있습니다.