2014-02-17 50 views
0

嗨即時解碼base64字符串數據並保存到像url格式的數據庫與時間戳,所以我需要壓縮解碼的數據和我的壓縮,如果未來的數據小於100kb然後不需要壓縮,否則將數據壓縮到50%的折扣。將base64編碼的字符串數據壓縮爲解碼格式

try 

{ 

String FileItemRefPath = propsFPCConfig.getProperty("fileCreationReferencePath"); 

String imageURLReferncePath = propsFPCConfig.getProperty("imageURLReferncePath"); 

File f = new File(FileItemRefPath+"/"+"productimages"+"/"+donorId); 

String strException = "Actual File "+f.getName(); 



     if(!f.exists()) 

     { 

      if (!f.mkdirs()) 

      { 
       System.out.println("direction creation failed"); 

       return null; 
      } 
     } 
     else 
     { 
      boolean isdirCreationStatus = f.mkdirs(); 
     } 

String strDateTobeAppended = new SimpleDateFormat("yyyyMMddhhmmss").format(new Date()); 

     String fileName = strImageName+strDateTobeAppended; 

     savedFile = new File(f.getAbsolutePath()+"/"+fileName); 


     strException=strException+" savedFile "+savedFile.getName(); 

     Base64 decoder = new Base64(); 

     byte[] decodedBytes = decoder.decode(strImageBase64); 

     if((decodedBytes != null) && (decodedBytes.length != 0)) 
     { 
     System.out.println("Decoded bytes length:"+decodedBytes.length); 

     fos = new FileOutputStream(savedFile); 

     System.out.println(new String(decodedBytes) + "\n") ; 

     int x=0; 
     { 
      fos.write(decodedBytes, 0, decodedBytes.length); 
     } 

     fos.flush(); 

     } 

     if(fos != null) 
     { 
      fos.close(); 

     System.out.println("file output stream"+savedFile.getName()); 

      return savedFile.getName(); 
     } 
     else 
     { 
      return null; 
     } 
    } 
    catch(Exception e) 
    { 

     e.printStackTrace(); 
    } 
    finally 
    { 
     try 
     { 
      if(fos!= null) 
      { 
       fos.close(); 
      } 
      else 
      { 
       savedFile = null; 
      } 
     } 
     catch (IOException e) 
     { 

     e.printStackTrace(); 
     } 
    } 
    return savedFile.getName(); 
} 
+0

我不太確定你的問題在這裏。您無法事先確定給定的八位字節流可以壓縮多少。如果你想壓縮數據,你總是可以使用LZMA算法。有一個純Java實現它的地方。去谷歌上查詢。 :) –

+0

對不起老闆,這裏即時獲取base64字符串數據,所以它是> 100 kb,所以我想壓縮數據 – user2964861

+0

那麼,爲什麼你base64的數據編碼?這增加了33%的開銷。如果你壓縮這個,結果將不再是base64編碼,你基本上回到原點。你想要準確表達什麼? –

回答

0

您的主要問題可能不會被壓縮在Java中。爲此,你可以簡單地使用諸如the Deflater class之類的東西。但是在iOS上它可能會更復雜一些,因爲我不確定你在iOS中有什麼樣的類似zlib的工具。