2015-12-03 101 views
1

我已經建立了通過創建壓縮文件來存檔我的應​​用程序的用戶數據的功能。Android創建的Zip不包含包含的文件大小

我創建壓縮文件是這樣的:

try 
{ 
    int iBufferSize = 2048; 
    int iByteCount = 0; 
    byte btData[] = new byte[iBufferSize]; 
    File fZipFile = new File(ARCHIVE_FILE); 
    FileOutputStream fos = new FileOutputStream(fZipFile); 
    ZipOutputStream zos = new ZipOutputStream(fos); 

    BufferedInputStream bis = null; 
    FileInputStream fIn = null; 
    ZipEntry ze = null; 
    File[] fPartitions = new File(FILES_PATH).listFiles(); 
    for(File fPartition : fPartitions) 
    { 
     lFileSize = fPartition.length(); 
     fIn = new FileInputStream(fPartition); 
     bis = new BufferedInputStream(fIn, iBufferSize); 
     ze = new ZipEntry(fPartition.getName()); 
     ze.setSize(lFileSize); 
     zos.putNextEntry(ze); 
     while((iByteCount = bis.read(btData, 0, iBufferSize)) != -1) 
     { 
      zos.write(btData, 0, iByteCount); 
     } 
     bis.close(); 
     zos.closeEntry(); 
    } 
    zos.close(); 

    bis.close(); 
    fIn.close(); 
    bOk = true; 
} 
catch(Exception e) 
{ 
    e.printStackTrace(); 
} 

現在,當我嘗試恢復的內容,我不能夠拿到原始文件的大小。這裏是我正在做的擴展:

try 
{ 
    // open the archive 
    FileInputStream fis = new FileInputStream(fArchive); 
    ZipInputStream zis = new ZipInputStream(new BufferedInputStream(fis)); 
    bBuffer = new byte[2048]; 

    // iterate through all files, putting them back into the same place 
    ze = zis.getNextEntry(); 
    while(ze != null) 
    { 
     strFileName = ze.getName(); 
     strFileName = FILE_PATH + "/" + strFileName; 
     fCheck = new File(strFileName); 
     lZipFileSize = ze.getSize(); // <--- returns -1 systematically 
     lTargetFileSize = fCheck.length(); 
     fCheck = null; 
     FileOutputStream fos = new FileOutputStream(strFileName); 

     // read the data 
     while((iCount = zis.read(bBuffer)) != -1) 
     { 
      fos.write(bBuffer, 0, iCount); 
     } 
     fos.close(); 

     ze = zis.getNextEntry(); 
    } 
    zis.close(); 
    fis.close(); 
    bOk = true; 
} 
catch(Exception e) 
{ 
    e.printStackTrace(); 
} 

我注意到,ze.getSize()將返回-1系統。

如何存儲和獲取單個文件大小?

回答

相關問題