base64转换成图片,并打ZIP包

时间:2023-01-31 22:56:06
String xml = "/9j/4AAQSkZJRgABAQEAYABgAAD/2wBDAAYEBQYFBAYGBQYHBwYIChAKCgkJChQODwwQFxQYGBcUFhYaHSUfGhsjHBYWICwgIyYnKSopGR8tMC0oMCUoKSj/2wBDAQcHBwoIChMKChMoGhYaKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCj/wAARCAYACAADASIAAhEBAxEB/8QAHwAAAQUBAQEBAQEAAAAAAAAAAAECAwQFBgcICQoL/8QAtRAAAgEDAwIEAwUFBAQAAAF9AQIDAAQRBRIhMUEGE1FhByJxFDKBkaEII0KxwRVS0fAkM2JyggkKFhcYGRolJicoKSo0NTY3ODk6Q0RFRkdISUpTVFVWV1hZWmNkZWZnaGlqc3R1dnd4eXqDhIWGh4iJipKTlJWWl5iZmqKjpKWmp6ipqrKztLW2t7i5usLDxMXGx8jJytLT1NXW19jZ2uHi4+Tl5ufo6erx8vP09fb3+Pn6/8QAHwEAAwEBAQEBAQEBAQAAAAAAAAECAwQFBgcICQoL/8QAtREAAgECBAQDBAcFBAQAAQJ3AAECAxEEBSExBhJBUQdhcRMiMoEIFEKRobHBCSMzUvAVYnLRChYkNOEl8RcYGRomJygpKjU2Nzg5OkNERUZHSElKU1RVVldYWVpjZGVmZ2hpanN0dXZ3eHl6goOEhYaHiImKkpOUlZaXmJmaoqOkpaanqKmqsrO0tba3uLm6wsPExcbHyMnK0tPU1dbX2Nna4uPk5ebn6Onq8vP09fb3";//为base64编码由于太多不好复制,在此在复制一半,此base64只为部分不能生成图片,如需要测试,请将base64完整化
String platFormFiles = "D:/image";
String fileName = "123456";
String zipName = "123456" +"-"+"20130719";
File file = new File(platFormFiles);//存放照片的文件
   if(!file.exists() || file.isFile()) {
    file.mkdirs();
   }
BASE64Decoder decoder = new BASE64Decoder();
// Base64解码
byte[] bytes = decoder.decodeBuffer(xml);
for (int i = 0; i < bytes.length; ++i) {
if (bytes[i] < 0) {// 调整异常数据
bytes[i] += 256;
}
}
// 生成jpeg图片
OutputStream out = new FileOutputStream(platFormFiles + "/" + zipName + ".jpg");
out.write(bytes);
out.flush();
out.close();
// 将文件压缩成zip
ZipOutputStream zip = new ZipOutputStream(new FileOutputStream(platFormFiles + "/" + zipName + ".zip"));
File s = new File(platFormFiles + "/" + zipName + ".jpg");
ZipEntry ze = new ZipEntry(fileName + ".jpg");
ze.setTime(s.lastModified());
zip.putNextEntry(ze);
InputStream ins = new FileInputStream(s);
byte[] b = new byte[1024];
while (ins.read(b) != -1) {
zip.write(b);
}
zip.flush();
zip.close();

File input = new File(platFormFiles + "/" + zipName + ".zip");