如何将二进制数据转换为Zip文件? [英] How to Convert Binary Data to Zip file?

查看:161
本文介绍了如何将二进制数据转换为Zip文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Right now I am working on a task to convert binary data in to a zip file

我正在呼叫url,并从类似服务器的服务器获得响应

I am calling a url and getting a response from server like

A @B Ar?E⏾ 7 ϫ f 걺N Yg o_M^ DDTUUX_ e.嗨\ � �ڂ(� �0 rm��'�ed���� �:6h�k�ڗ� ���fnp���7��)��:��N�U�viR�,) II����M Np M 7 n !A!)))AAFAq)Q).y 是的. ֞ ͅ_ O nc f w ʰ 6 3 2 ZZZZN0 O { mC $ ,> CW/)?? ٥ ߗ d =?R J* E {2L W __PRR _@ _H : Ə Ջ J ^v 0wo + o -Ä@ R?6? P ( 0 WP?j k. C·E

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

现在,我想将这些数据保存到zip文件中,我已经进行了很多搜索并找到了一些链接,但是没有达到目标.

now I want to save this data to zip file i have searched a lot and find some links but not meet the goal.

我在这里完成

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

如果您对此有任何想法,请指导我.

please guid me if you have any idea about this.

推荐答案

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

All I need to do is construct a BufferedInputStream from the entity. Just replace BufferedReader with a BufferedInputStream. I would recommend using ISO-8859-1 .An underlying streaming encoder to read binary data is a waste of processing power.

private class methodName extends
            AsyncTask<String, Integer, byte[]> {

        @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)
    {

    }
    }

这篇关于如何将二进制数据转换为Zip文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆