2010-12-27 4 views
2

누구도 파일을 압축하는 방법을 알고 있습니까 ZipOutputStream?BlackBerry Java 응용 프로그램의 파일 압축

try { 
    // Creating Zip Streams 
    FileConnection path = (FileConnection) Connector.open(
      "file:///SDCard/BlackBerry/documents/" + "status.zip", 
      Connector.READ_WRITE); 

    if (!path.exists()) { 
     path.create(); 
    } 
    ZipOutputStream zinstream = new ZipOutputStream(
      path.openOutputStream()); 

    // Adding Entries 
    FileConnection jsonfile = (FileConnection) Connector.open(
      "file:///SDCard/BlackBerry/documents/" + "status.json", 
      Connector.READ_WRITE); 
    if (!jsonfile.exists()) { 
     jsonfile.create(); 
    } 
    int fileSize = (int) jsonfile.fileSize(); 
    if (fileSize > -1) { 
     byte[] data = new byte[fileSize]; 
     InputStream input = jsonfile.openInputStream(); 
     input.read(data); 

     ZipEntry entry = new ZipEntry(jsonfile.getName()); 
     zinstream.putNextEntry(entry); 
     // zinstream.write(buf); 
     // ZipEntry entry = null; 

     path.setWritable(true); 
     OutputStream out = path.openOutputStream(); 

     int len; 
     while ((len = input.read(data)) != -1) { 
      out.write(data, 0, len); 
      out.flush(); 
      out.close(); 
      zinstream.close(); 
      content = "FILE EXIST" + entry; 
     } 

     jsonfile.close(); 
     path.close(); 
    } 
} catch (...) { 
    ... 
} 
+1

무엇이 오류입니까? – UVM

답변

3

데이터는 ZipOutputStream이zinstream 대신의 새로운 의 OutputStream밖으로에 기록되어야한다. 작성 완료 후도

그것의 중요한 것은 ZipEntry를항목를 닫습니다.

FileConnection path = (FileConnection) Connector.open(
     "file:///SDCard/BlackBerry/documents/" + "status.zip", 
     Connector.READ_WRITE); 

if (!path.exists()) { 
    path.create(); 
} 
ZipOutputStream zinstream = new ZipOutputStream(path.openOutputStream()); 

// Adding Entries 
FileConnection jsonfile = (FileConnection) Connector.open(
     "file:///SDCard/BlackBerry/documents/" + "status.json", 
     Connector.READ_WRITE); 
if (!jsonfile.exists()) { 
    jsonfile.create(); 
} 
int fileSize = (int) jsonfile.fileSize(); 
if (fileSize > -1) { 
    InputStream input = jsonfile.openInputStream(); 
    byte[] data = new byte[1024]; 

    ZipEntry entry = new ZipEntry(jsonfile.getName()); 
    zinstream.putNextEntry(entry); 

    int len; 
    while ((len = input.read(data)) > 0) { 
     zinstream.write(data, 0, len); 
    } 
    zinstream.closeEntry(); 
} 
jsonfile.close(); 
zinstream.close(); 
path.close(); 
+0

정말 고마워요. 이제 완벽하게 작동합니다. 출력이 OutputStream 대신 ZipOutputStream이어야한다는 것을 깨달았습니다. – JohnDoe4136

2

은 블랙 베리 등 ZipOutputStream이와 ZipEntry를 및 관련 클래스와 J2SE 클래스의 모든이없는 J2ME API를 사용합니다. 도움이 될 수도있는 ZLibOutputStream과 같은 클래스가 있지만 바이트 수준의 압축 일 뿐이므로 실제 PKZIP 컨테이너를 직접 구현해야합니다. (타사 라이브러리가 없으면이 작업을 수행 할 수 있습니다. 당신).

+1

이 작업을 위해 ZipME를 다운로드했습니다. 조언 해주셔서 감사합니다! – JohnDoe4136

관련 문제