如何将二进制数据转换为Zip文件?

现在我正在处理将二进制数据转换为zip文件的任务

我正在调用一个URL并从服务器获得响应

A @B ArE⏾ 7 ϫ f N걺Yg o_M^ D T UX_ e? hi \ ڂ( 0 rm ' ed :6h k ڗ fnp 7 ) : N U viR ,) II M II M Np M 7 n !A!))AAFAq)Q) yy . ? ֞ Ɲ O nc f w ʰ 6 32 ƢZZ N0 O{ mC $ ,> CW /)? ?5 ߗ d = R J* E {2L ח W ӑ_PRR _@ _H : Ə Ջ J ^v 0wo + o -Ä@ R6 P ( 0 WPSj k C E

现在我想将这些数据保存到zip文件我已搜索了很多并找到一些链接但不符合目标。

我已经完成了

 OutputStreamWriter osw = new OutputStreamWriter(openFileOutput( "products.zip", Context.MODE_PRIVATE)); osw.write(data); osw.close(); 

如果你对此有任何想法,请告诉我。

OutputStreamWriter osw

没有!

Writer用于编写文本 ,而不是二进制文本

首先,它看起来像你也阅读文本,你不应该。

使用InputStream读取原始内容,使用OutputStream写入文件:

 final OutputStream out = /* open your file using a FileOutputStream here */; final byte[] buf = new byte[8096]; // size as appropriate // "in" is the InputStream from the socket int count; try { while ((count = in.read(buf)) != -1) out.write(buf, 0, count); out.flush(); } finally { out.close(); } 

读者并不打算阅读八位字节流。

从字符输入流中读取文本,缓冲字符,以便有效地读取字符,数组和行。

您正在寻找BufferedInputStream。

HttpEntity上的getContent()方法返回一个InputStream。 将它包装在BufferedInputStream周围并将其写入文件或ByteArrayOutputStream。

  byte[] buffer = new byte[5 * 1024]; int numRead = -1; while( (numRead = bufferedInputStream.read(buffer))!= -1) { byteArrayOutputStream.write(buffer, 0, numRead); } byteArrayOutputStream.flush(); byteArrayOutputStream.close(); byte[] result = byteArrayOutputStream.toByteArray(); 

为了节省内存,我建议您写入BufferedOutputStream,而不是尝试将流中的字节转换为数据结构。 Android设备可能会耗尽大量zip文件的内存。

我需要做的就是从实体构造一个BufferedInputStream。 只需用BufferedInputStream替换BufferedReader即可。 我建议使用ISO-8859-1。底层流编码器读取二进制数据是浪费处理能力。

 private class methodName extends AsyncTask { @Override protected byte[] doInBackground(String... params) { String uri = params[0]; try { MultipartEntityBuilder entity; File f; FileBody fb; entity = MultipartEntityBuilder.create(); entity.setMode(HttpMultipartMode.BROWSER_COMPATIBLE); f = new File(zipImageFile); fb = new FileBody(f); entity.addPart("orderFile", fb); HttpClient httpclient = new DefaultHttpClient(); HttpPost httppost = new HttpPost(uri); Log.e("Uploload Missing Image URL", "" + uri); httppost.setEntity(entity.build()); HttpResponse response = httpclient.execute(httppost); // byte[] fileBites=null; BufferedInputStream bufferedInputStream; ByteArrayOutputStream byteArrayOutputStream; byte[] buffer = new byte[5 * 1024]; int numRead = -1; while( (numRead = bufferedInputStream.read(buffer))!= -1) { byteArrayOutputStream.write(buffer, 0, numRead); } byteArrayOutputStream.flush(); byteArrayOutputStream.close(); byte[] result = byteArrayOutputStream.toByteArray(); // fileBites=stringBuffer.toString().getBytes(); // Log.e("FILE BITES", fileBites+"=>"+fileBites.length); return ; // return stringBuffer.toString(); } catch (Exception e) { return e.toString().getBytes(); } } @Override protected void onPostExecute(byte[] result) { // TODO Auto-generated method stub super.onPostExecute(result); Log.e("Response From Server", "" + result); writeToFile(result); } } private void writeToFile(byte[] data) { try { FileOutputStream fop = null; File file; file = new File(AppConstants.DataPath+"/products.zip"); fop = new FileOutputStream(file); // if file doesnt exists, then create it if (!file.exists()) { file.createNewFile(); } try { fop.write(data); } catch (IOException e) { Log.e("Exception", "File write failed: " + e.toString()); } unzipImage(AppConstants.DataPath + "/products.zip", AppConstants.DataPath); }catch (Exception E) { } }