2014-02-17 51 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的工具。