import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import java.util.Base64;
public class UnzipDecodeExample {
public static void main(String[] args) {
String input = "UEsDBBQACAgIAEFcjFkAAAAAAAAAAAAAAAABAAAAMEtMSjY0MgYAUEsHCFy7As8IAAAABgAAAA==";
String result = unzipDecode(input, true);
System.out.println("Decoded and Unzipped String:");
System.out.println(result);
}
public static String unzipDecode(String zipStr, boolean isZip) {
String unzipStr = "";
try {
byte[] unzip = Base64.getDecoder().decode(zipStr);
if (isZip) {
unzipStr = decompress(unzip);
} else {
unzipStr = new String(unzip, "UTF-8");
}
} catch (Exception e) {
System.out.println(zipStr + ",解压解密失败: " + e.getCause());
e.printStackTrace();
}
return unzipStr;
}
private static String decompress(byte[] compressed) {
if (compressed == null) {
return null;
}
ByteArrayOutputStream out = null;
ByteArrayInputStream in = null;
ZipInputStream zin = null;
String decompressed = null;
try {
out = new ByteArrayOutputStream();
in = new ByteArrayInputStream(compressed);
zin = new ZipInputStream(in);
ZipEntry entry = zin.getNextEntry();
if (entry != null) {
byte[] buffer = new byte[1024];
int offset;
while ((offset = zin.read(buffer)) != -1) {
out.write(buffer, 0, offset);
}
decompressed = out.toString("UTF-8");
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (zin != null) zin.close();
if (in != null) in.close();
if (out != null) out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return decompressed;
}
}