的Base64恩code文件和COM preSS它 [英] Base64-encode a file and compress it

查看:240
本文介绍了的Base64恩code文件和COM preSS它的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的目标是EN code的文件,并在Java文件夹中压缩它。我必须使用Apache的Commons- codeC库。我能够连接code和压缩它,它工作正常,但是当我去code将其恢复到原来的形态,它看起来像该文件还没有完全被EN codeD。看起来像一些部件丢失。谁能告诉我,为什么出现这种情况?

我还附上我的code的一部分,供大家参考,让您可以指导我相应。

 私人无效的zip(){
    INT BUFFER_SIZE = 4096;
    字节[]缓冲区=新的字节[BUFFER_SIZE];    尝试{
        //创建ZIP文件
        串outFilename =H:\\\\ OUTPUT.zip;
        ZipOutputStream出=新ZipOutputStream(新的FileOutputStream(
                outFilename));        //的COM preSS文件
        对于(INT I:list.getSelectedIndices()){
            的System.out.println(vector.elementAt(I));
            在的FileInputStream =新的FileInputStream(vector.elementAt(I));
            文件f = vector.elementAt(ⅰ);            //添加ZIP条目输出流。
            out.putNextEntry(新的ZipEntry(f.getName()));            //转自文件到ZIP文件字节
            INT LEN;            而((LEN = in.read(缓冲液))大于0){
                缓冲= org.apache.commons。codec.binary.Base64
                        .EN codeBase64(缓冲);
                out.write(缓冲液,0,LEN);            }            //完成输入
            out.closeEntry();
            附寄();        }        //完成ZIP文件
        out.close();
    }赶上(IOException异常五){
        的System.out.println(捕获异常);
        e.printStackTrace();
    }
}


解决方案

BASE64 EN codeD数据通常比源长,然而您正在使用的源数据的长度来写EN coded到输出流

您已经生成的数组,而不是你的变量的使用尺寸LEN

第二次通知 - 不要重新定义缓存每次连接code一个字节。只要写结果到输出。

 ,而((LEN = in.read(缓冲))0){
     字节[] = ENC Base64.en codeBase64(Arrays.copyOf(缓冲,LEN));
     out.write(ENC,0,enc.length);
 }

更新:使用 Arrays.copyOf(... )以设置用于编码的输入缓冲器的长度。

My goal is to encode a file and zip it in a folder in java. I have to use the Apache's Commons-codec library. I am able to encode and zip it and it works fine but when i decode it back to its original form, it looks like the file has not completely been encoded. Looks like a few parts are missing. Can anybody tell me why this happens?

I am also attaching the part of my code for your reference so that you can guide me accordingly.

private void zip() {
    int BUFFER_SIZE = 4096;
    byte[] buffer = new byte[BUFFER_SIZE];

    try {
        // Create the ZIP file
        String outFilename = "H:\\OUTPUT.zip";
        ZipOutputStream out = new ZipOutputStream(new FileOutputStream(
                outFilename));

        // Compress the files
        for (int i : list.getSelectedIndices()) {
            System.out.println(vector.elementAt(i));
            FileInputStream in = new FileInputStream(vector.elementAt(i));
            File f = vector.elementAt(i);

            // Add ZIP entry to output stream.
            out.putNextEntry(new ZipEntry(f.getName()));

            // Transfer bytes from the file to the ZIP file
            int len;

            while ((len = in.read(buffer)) > 0) {
                buffer = org.apache.commons.codec.binary.Base64
                        .encodeBase64(buffer);
                out.write(buffer, 0, len);

            }

            // Complete the entry
            out.closeEntry();
            in.close();

        }

        // Complete the ZIP file
        out.close();
    } catch (IOException e) {
        System.out.println("caught exception");
        e.printStackTrace();
    }
}

解决方案

BASE64 encoded data are usually longer than source, however you are using the length of the source data to write encoded to output stream.

You have use size of the generated array instead of your variable len.

Second notice - do not redefine buffer each time you encode a byte. Just write result into output.

 while ((len = in.read(buffer)) > 0)  {                         
     byte [] enc = Base64.encodeBase64(Arrays.copyOf(buffer, len));
     out.write(enc, 0, enc.length);
 }

UPDATE: Use Arrays.copyOf(...) to set length of the input buffer for encoding.

这篇关于的Base64恩code文件和COM preSS它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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