2012-07-02 2 views
3

У меня есть zip-файл, содержащий папку, и внутри папки у меня есть файл с изображениями. Я хочу извлечь эти изображения. Однако я ничего не смог найти. Я смотрел на zipMe, но не смогли найти соответствующую помощь.UnZip/Extract Zip-файл в blackberry

Ниже приведен код, который я разработал до сих пор.

ZipEntry dataZE; 
    InputStream isData; 
    StringBuffer sbData; 
    ZipInputStream dataZIS; 

    String src = "file:///store/home/user/images.zip"; 
    String path = "file:///store/home/"; 
    String fileName = ""; 
    FileConnection f_Conn; 

    public UnZipper() { 

     debugger("Unzipper constructor"); 

     try { 
      f_Conn = (FileConnection) Connector.open(src); 
     } catch (IOException e) { 
      debugger("f_conn error :" + e.getMessage()); 
     } 

     try { 
      isData = f_Conn.openInputStream(); 
     } catch (IOException e) { 
      debugger("f_conn error getting ip_stream:" + e.getMessage()); 
     } 

     sbData = new StringBuffer(); 

     dataZIS = new ZipInputStream(isData); 

     debugger("got all thing initialized"); 

    } 

    public void run() { 

     debugger("unzipper run"); 

     try { 
      startUnziping(); 
     } catch (IOException e) { 

      debugger("Error unzipping " + e.getMessage()); 
     } 

     debugger("finished..."); 

    } 


    private void startUnziping() throws IOException { 

     debugger("startUnziping"); 
     dataZE = dataZIS.getNextEntry(); 
     fileName = dataZE.getName(); 
     writeFile(); 
     dataZIS.closeEntry(); 

     debugger(">>>>>>>>>>> : " + fileName); 
    } 

    private void readFile() throws IOException { 

     debugger("readFile"); 
     int ch; 
     int i = 0; 
     while ((ch = dataZIS.read()) != -1) { 

      debugger((i++) + " : " + sbData.toString() 
        + " >>> writting this.."); 
      sbData.append(ch); 
     } 

    } 

    private void writeFile() { 

     debugger("writting file..."); 
     FileConnection f_Conn = null; 
     byte[] file_bytes = new byte[sbData.length()]; 
     file_bytes = sbData.toString().getBytes(); 

     try { 
      readFile(); 
     } catch (IOException e) { 
      debugger("Error while reading " + e.getMessage()); 
     } 

     try { 
      f_Conn = (FileConnection) Connector.open(path + fileName); 
     } catch (IOException e) { 
      debugger("getting f_conn" + e.getMessage()); 
     } 

     if (!f_Conn.exists()) { 
      // create the file first 
      debugger("I know file does not exists"); 
      try { 
       f_Conn.mkdir(); 
      } catch (IOException e) { 
       debugger("Oops!!! error creating fle : " + e.getMessage()); 
      } 
     } 
     try { 
      f_Conn.setWritable(true); 
      debugger("file is nt writeable"); 
     } catch (IOException e) { 
      debugger("cannot make it writeable : " + e.getMessage()); 
     } 
     OutputStream lo_OS = null; 
     try { 
      lo_OS = f_Conn.openOutputStream(); 
      debugger("got out Stream hero!!!"); 
     } catch (IOException e) { 
      debugger("cant get out Stream !!!"); 
     } 
     try { 
      lo_OS.write(file_bytes); 
      debugger("yess...writtent everything"); 
     } catch (IOException e) { 
      add(new LabelField("Error writing file ..." + e.getMessage())); 
     } 
     try { 
      lo_OS.close(); 
      debugger("now closing connection..."); 
     } catch (IOException e) { 
      debugger("error closing out stream : " + e.getMessage()); 
     } 

    } 
} 

Я был в состоянии получить ZipEntry, представляющий папку, которая содержит изображения, однако я не смог понять, как я должен извлечь эти изображения.

Спасибо за помощь.

ответ

4

Истерируйте все ZipEntry в zip-файле у вас startUnzipping (вы работаете только с первым в своем коде). Пункт соответствует ребенку файл должен иметь имя, как «FOLDERNAME/имя файла».

Смежные вопросы