现在我正在执行一项将二进制数据转换为 zip 文件的任务
我正在调用一个网址并从服务器获取响应,例如
A@B�Ar E⏾�7�ϫ���f�걺N�����Yg��o_M^�D�T�U X_�e?�嗨\ ��(� �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���
�-�@�R 6 �P�(���0�WP j�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();
}
关于java - 如何将二进制数据转换为 Zip 文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22778509/