英文:
Extract the zip and convert to base 64 of each file
问题
public class UnzipUtility {
private static final int BUFFER_SIZE = 4096;
private static void extractFile(ZipInputStream zipIn, ZipEntry entry) throws IOException {
ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); // Create a ByteArrayOutputStream
byte[] bytesIn = new byte[BUFFER_SIZE];
System.out.println("File Name: " + entry.getName());
int read = 0;
while ((read = zipIn.read(bytesIn)) != -1) {
outputStream.write(bytesIn, 0, read); // Write bytes to the output stream
}
String base64Data = Base64.encodeBase64String(outputStream.toByteArray()); // Convert to base64
outputStream.close();
// Now you have the base64Data for the attached file
}
public static void main(String[] args) throws IOException {
String attachmentVariable = "zip base 64 data";
byte[] bytedata = attachmentVariable.getBytes("UTF-8");
byte[] valueDecoded = Base64.decodeBase64(bytedata);
ZipInputStream zipIn = new ZipInputStream(new ByteArrayInputStream(valueDecoded));
ZipEntry entry = zipIn.getNextEntry();
// Iterates over entries in the zip file
while (entry != null) {
extractFile(zipIn, entry);
zipIn.closeEntry();
entry = zipIn.getNextEntry();
}
}
}
英文:
I am reading the list of attachment from a system witch returns the attached document in base 64 encoded string as a zip and My objective is to get the base 64 encoded string for each attached document.
Note:- I am trying below code where I am unzipping the zip and writing at my local file system.
. But in real I wanted to get the base 64 format for each file without writing the file in local file system.
public class UnzipUtility {
private static final int BUFFER_SIZE = 4096;
private static void extractFile(ZipInputStream zipIn, ZipEntry entry) throws IOException {
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("D:/Project/"+File.separator+entry.getName()));
byte[] bytesIn = new byte[BUFFER_SIZE];
System.out.println("File Name "+entry.getName());
int read = 0;
while ((read = zipIn.read(bytesIn)) != -1) {
//Hear I dont not want to write the output stream insted I want to get the base64 data for each file.
bos.write(bytesIn);
}
bos.close();
}
public static void main(String[] args) throws IOException {
String attachmentVariable="zip base 64 data"
byte[] bytedata = attachmentVariable.getBytes("UTF-8");
byte[] valueDecoded = Base64.decodeBase64(bytedata);
ZipInputStream zipIn = new ZipInputStream(new ByteArrayInputStream(valueDecoded));
ZipEntry entry = zipIn.getNextEntry();
// iterates over entries in the zip file
while (entry != null) { extractFile(zipIn,entry);
zipIn.closeEntry();
entry = zipIn.getNextEntry();
}
}
}
答案1
得分: 0
以下是翻译好的内容:
要在数据写入OutputStream
时进行Base64编码,可以使用Encoder.wrap(OutputStream os)
方法。
默认情况下,BufferedOutputStream
会使用8192字节的缓冲区,因此如果将BUFFER_SIZE
增加到8192,则不需要BufferedOutputStream
。
您应该使用try-with-resources和更新的NIO.2 API。
这意味着您的代码应为:
private static final int BUFFER_SIZE = 8192;
private static void extractFile(ZipInputStream zipIn, ZipEntry entry) throws IOException {
try (OutputStream fos = Files.newOutputStream(Paths.get("D:/Project", entry.getName()));
OutputStream b64os = Base64.getEncoder().wrap(fos); ) {
System.out.println("File Name " + entry.getName());
byte[] buf = new byte[BUFFER_SIZE];
for (int len = 0; (len = zipIn.read(buf)) != -1; ) {
b64os.write(buf, 0, len);
}
}
}
英文:
To Base64 encode data as it is being written to an OutputStream
, use the Encoder.wrap(OutputStream os)
method.
By default, BufferedOutputStream
will use a 8192-byte buffer, so if you increase BUFFER_SIZE
to 8192, then you won't need the BufferedOutputStream
.
You should use try-with-resources, and the newer NIO.2 API.
Which means your code should be:
private static final int BUFFER_SIZE = 8192;
private static void extractFile(ZipInputStream zipIn, ZipEntry entry) throws IOException {
try ( OutputStream fos = Files.newOutputStream(Paths.get("D:/Project", entry.getName()));
OutputStream b64os = Base64.getEncoder().wrap(fos); ) {
System.out.println("File Name " + entry.getName());
byte[] buf = new byte[BUFFER_SIZE];
for (int len = 0; (len = zipIn.read(buf)) != -1; ) {
b64os.write(buf, 0, len);
}
}
}
答案2
得分: 0
String base64ZipFile = "zip base 64 data";
Map<String, String> base64Entries = new LinkedHashMap<>();
try (ZipInputStream zipIn = new ZipInputStream(new ByteArrayInputStream(Base64.getDecoder().decode(base64ZipFile)))) {
Encoder encoder = Base64.getEncoder();
for (ZipEntry entry; (entry = zipIn.getNextEntry()) != null; ) {
base64Entries.put(entry.getName(), encoder.encodeToString(zipIn.readAllBytes()));
}
}
英文:
So, you have a Base64 encoded string with a zip file, and you want a Map<String, String>
, where key is zip entry name and value is the Base64 encoded content.
In Java 9+, that is easily done like this:
String base64ZipFile = "zip base 64 data";
Map<String, String> base64Entries = new LinkedHashMap<>();
try (ZipInputStream zipIn = new ZipInputStream(new ByteArrayInputStream(Base64.getDecoder().decode(base64ZipFile)))) {
Encoder encoder = Base64.getEncoder();
for (ZipEntry entry; (entry = zipIn.getNextEntry()) != null; ) {
base64Entries.put(entry.getName(), encoder.encodeToString(zipIn.readAllBytes()));
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论