2012-06-20 3 views
2

URL에서 여러 파일을 읽을 수 있습니다 (즉, pdf, txt, tiff 등의 형식 일 수 있음). ZipOutputStream을 사용하여 압축합니다. 내 코드는 다음과 같습니다메모리에서 파일 zip을

// using in-memory file read 
    // then zipping all these files in-memory 
    ByteArrayOutputStream baos = new ByteArrayOutputStream(); 
    ZipOutputStream zos = new ZipOutputStream(baos); 
    ..... 

    URL url = new URL(downloadUrl); // can be multiple URLs 

    ByteArrayOutputStream bais = new ByteArrayOutputStream(); 
    InputStream is = url.openStream(); 
    byte[] byteChunk = new byte[4096]; 
    int n; 

    while ((n = is.read(byteChunk)) > 0) 
    { 
     bais.write(byteChunk, 0, n); 
    } 

    byte[] fileBytes = bais.toByteArray(); 

    ZipEntry entry = new ZipEntry(fileName); 
    entry.setSize(fileBytes.length); 

    zos.putNextEntry(entry); 
    zos.write(fileBytes); 
    zos.closeEntry(); 

    // close the url input stream 
    is.close(); 

    // close the zip output stream 
zos.close(); 


    // read the byte array from ByteArrayOutputStream 
    byte[] zipFileBytes = baos.toByteArray(); 

    String fileContent = new String(zipFileBytes); 

내가 다음 내 펄 프론트 엔드 응용 프로그램이 컨텐츠 "를 포함한 FileContent"을 통과하고있다.

그리고이 압축 파일 다운로드 펄 코드를 사용하고 있습니다 :

WPHTTPResponse::setHeader('Content-disposition', 'attachment; filename="test.zip"'); 
WPHTTPResponse::setHeader('Content-type', 'application/zip'); 
print $result; // string coming from java application 

을하지만 지퍼가 손상되어주고있다 파일. 데이터 변환에 문제가 있다고 생각합니다.

어떤 도움을 주셔서 감사합니다.

+0

Windows를 사용하는 경우는? – mob

+0

번호. 리눅스를 사용. – Kevindra

답변

5

문제는 문자열로 zip 바이트를 출력 할 수 있다고 생각하는 것입니다. 이 문자열은 zip 컨텐츠를 다시 재생하는 데 사용할 수 없습니다. 원시 바이트를 사용하거나 바이트를 base64 인코딩과 같이 문자열로 표현할 수있는 코드로 인코딩해야합니다.

+0

고마워, 나는 그것을 시도 할 것이다. – Kevindra

+0

고마워요 로저. 그것은 효과가있다! :) – Kevindra