public static void main(String[] args) {
String path = "D:/a.zip";
try {
byte[] data = "abcd".getBytes();
FileOutputStream os = new FileOutputStream(path);
ZipOutputStream zos = new ZipOutputStream(os);
ZipEntry entry = new ZipEntry("a.txt");
zos.putNextEntry(entry);
zos.write(data);
zos.close();
os.close();
System.out.println("压缩完成");
//
FileInputStream is = new FileInputStream(path);
ZipInputStream zis = new ZipInputStream(is);
entry = zis.getNextEntry();
String s = String.format("name=%s; size=%d", entry.getName(), entry.getSize());
if (entry.getSize() == -1) {
s = "解压失败:" + s;
} else {
s = "解压成功:" + s;
}
System.out.println(s);
zis.closeEntry();
zis.close();
is.close();
} catch (Throwable e) {
throw new RuntimeException(e);
}
}
谁帮我看看为什么解压失败。用windows解压是可以解压出文件来的。
String path = "D:/a.zip";
try {
byte[] data = "abcd".getBytes();
FileOutputStream os = new FileOutputStream(path);
ZipOutputStream zos = new ZipOutputStream(os);
ZipEntry entry = new ZipEntry("a.txt");
zos.putNextEntry(entry);
zos.write(data);
zos.close();
os.close();
System.out.println("压缩完成");
//
FileInputStream is = new FileInputStream(path);
ZipInputStream zis = new ZipInputStream(is);
entry = zis.getNextEntry();
String s = String.format("name=%s; size=%d", entry.getName(), entry.getSize());
if (entry.getSize() == -1) {
s = "解压失败:" + s;
} else {
s = "解压成功:" + s;
}
System.out.println(s);
zis.closeEntry();
zis.close();
is.close();
} catch (Throwable e) {
throw new RuntimeException(e);
}
}
谁帮我看看为什么解压失败。用windows解压是可以解压出文件来的。
FileInputStream is = new FileInputStream(path);
ZipInputStream zis = new ZipInputStream(is);
entry = zis.getNextEntry();
byte[] buf = new byte[1024];
int len;
while ((len = zis.read(buf)) > 0) {
System.out.println(new String(buf,0, len));
}
String s = String.format("name=%s; size=%d", entry.getName(), entry.getSize());
if (entry.getSize() == -1) {
s = "解压失败:" + s;
} else {
s = "解压成功:" + s;
}
System.out.println(s);
zis.closeEntry();
zis.close();
is.close();