私の目標は、ファイルをエンコードし、Java のフォルダーに圧縮することです。Apache の Commons-codec ライブラリを使用する必要があります。エンコードして圧縮することができ、正常に動作しますが、デコードして元の形式に戻すと、ファイルが完全にエンコードされていないように見えます。いくつかの部品が欠落しているようです。なぜこれが起こるのか誰か教えてもらえますか?
また、参照用にコードの一部を添付して、それに応じてガイドできるようにします。
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();
}
}