使用 ZipOutputStream 将字符串写入 zip 文件
writing a string to a zip file using ZipOutputStream
最终目标只是压缩一个字符串并将其作为字符串 java
public void zip() {
try {
String myTestString = "a zip file test and another test";
InputStream in = new ByteArrayInputStream(myTestString.getBytes());
OutputStream out = new ByteArrayOutputStream();
ZipOutputStream zipOut = new ZipOutputStream(out);
ZipEntry zipEntry = new ZipEntry("wtf.txt");
zipOut.putNextEntry(zipEntry);
zipOut.write(myTestString.getBytes(),0,myTestString.getBytes().length);
zipOut.close();
myTestString = out.toString();
out.close();
System.out.println(myTestString);
// just a test if I can read the file
in = new ByteArrayInputStream(myTestString.getBytes());
out = new FileOutputStream("c:\t\string.zip");
byte[] allBytes = new byte[(int) myTestString.getBytes().length];
in.read(allBytes);
out.write(allBytes);
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
我发现我可以使用
将字符串写入压缩文件
public void zipStringToFile2() {
try {
FileOutputStream fos = new FileOutputStream("c:/t/compressed.zip");
ZipOutputStream zipOut = new ZipOutputStream(fos);
String myTestString = "a zip file test and another test";
int buffer = myTestString.getBytes().length;
// byte[] myBytes = myTestString.getBytes();
ByteArrayOutputStream out = new ByteArrayOutputStream(buffer);
ZipEntry zipEntry = new ZipEntry("wtf.txt");
zipOut.putNextEntry(zipEntry);
zipOut.write(myTestString.getBytes(),0,buffer);
zipOut.close();
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
但我无法让 zipOut 的输出写入字符串,然后将字符串写入文件,然后通过 OS 打开 zip 文件。怎么做到的?
myTestString = out.toString();
这不是您想要的。字节不是字符串。 toString() 不提供有用的信息(它是一个调试工具)。保留字节数组 (.toByteArray()
).
out.close();
在您检索到数据后关闭流?不要那样做。先关闭。 (这并不重要,在这里。ByteArrayXStream 的 close() 根本不做任何事情。关键是,它要么什么也不做,在这种情况下你应该删除它,或者如果它确实有效果,你的代码就会被破坏) .
myTestString.getBytes()
不,永远不要调用该方法。它通过使用 'platform default encoding' 将字符解码为字节来为您提供字节。谁知道那是什么。
} catch (IOException e) {
e.printStackTrace();
}
正确的 'I dunno' 异常处理程序是 throw new RuntimeException("unhandled", e);
,而不是 e.printStackTrace();
。你得到更多的信息,你得到更少的 WTF(因为你的将继续执行,即使事情明显错误,这是一个非常糟糕的主意)。
but I cannot get the output of zipOut to write to a string
是的。字符串不是字节,你想要的根本不可能。用任何语言。有些语言让它看起来像你可以 - 这些是糟糕的语言,将字符串和字节数组混为一谈。 Python 决定崩溃并燃烧并发明一个全新的 python (python2k -> python3k) 来尝试解决这个问题,这会显示出来。天啊,真是太痛苦了,他们为了解决这个疏忽而受苦了。
but I cannot get the output of zipOut to write to a string and then have the string write to a file and then via the OS open the zipfile. How can that be done?
因此,用 'byte array' 替换该句子中所有出现的 'string',一切都很好!
这正是我要找的:
public void zip() {
try {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ZipOutputStream zipOut = new ZipOutputStream(baos);
ByteArrayInputStream bais = new ByteArrayInputStream(testString.getBytes());
System.out.println("testString " + testString + " testString.getBytes() " + testString.getBytes());
ZipEntry zipEntry = new ZipEntry("Results.xml");
zipOut.putNextEntry(zipEntry);
byte[] bytes = new byte[1024];
int length;
while((length = bais.read(bytes)) >= 0 ) {
zipOut.write(bytes, 0, length);
}
zipOut.close();
bais.close();
baos.close();
byte[] zipBytes = baos.toByteArray();
// just a test to see if can be opened in the operating system
OutputStream os = new FileOutputStream(new File("c:/t/again.zip"));
os.write(zipBytes);
os.close();
} catch(IOException e) {
e.printStackTrace();
}
}
最终目标只是压缩一个字符串并将其作为字符串 java
public void zip() {
try {
String myTestString = "a zip file test and another test";
InputStream in = new ByteArrayInputStream(myTestString.getBytes());
OutputStream out = new ByteArrayOutputStream();
ZipOutputStream zipOut = new ZipOutputStream(out);
ZipEntry zipEntry = new ZipEntry("wtf.txt");
zipOut.putNextEntry(zipEntry);
zipOut.write(myTestString.getBytes(),0,myTestString.getBytes().length);
zipOut.close();
myTestString = out.toString();
out.close();
System.out.println(myTestString);
// just a test if I can read the file
in = new ByteArrayInputStream(myTestString.getBytes());
out = new FileOutputStream("c:\t\string.zip");
byte[] allBytes = new byte[(int) myTestString.getBytes().length];
in.read(allBytes);
out.write(allBytes);
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
我发现我可以使用
将字符串写入压缩文件 public void zipStringToFile2() {
try {
FileOutputStream fos = new FileOutputStream("c:/t/compressed.zip");
ZipOutputStream zipOut = new ZipOutputStream(fos);
String myTestString = "a zip file test and another test";
int buffer = myTestString.getBytes().length;
// byte[] myBytes = myTestString.getBytes();
ByteArrayOutputStream out = new ByteArrayOutputStream(buffer);
ZipEntry zipEntry = new ZipEntry("wtf.txt");
zipOut.putNextEntry(zipEntry);
zipOut.write(myTestString.getBytes(),0,buffer);
zipOut.close();
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
但我无法让 zipOut 的输出写入字符串,然后将字符串写入文件,然后通过 OS 打开 zip 文件。怎么做到的?
myTestString = out.toString();
这不是您想要的。字节不是字符串。 toString() 不提供有用的信息(它是一个调试工具)。保留字节数组 (.toByteArray()
).
out.close();
在您检索到数据后关闭流?不要那样做。先关闭。 (这并不重要,在这里。ByteArrayXStream 的 close() 根本不做任何事情。关键是,它要么什么也不做,在这种情况下你应该删除它,或者如果它确实有效果,你的代码就会被破坏) .
myTestString.getBytes()
不,永远不要调用该方法。它通过使用 'platform default encoding' 将字符解码为字节来为您提供字节。谁知道那是什么。
} catch (IOException e) { e.printStackTrace(); }
正确的 'I dunno' 异常处理程序是 throw new RuntimeException("unhandled", e);
,而不是 e.printStackTrace();
。你得到更多的信息,你得到更少的 WTF(因为你的将继续执行,即使事情明显错误,这是一个非常糟糕的主意)。
but I cannot get the output of zipOut to write to a string
是的。字符串不是字节,你想要的根本不可能。用任何语言。有些语言让它看起来像你可以 - 这些是糟糕的语言,将字符串和字节数组混为一谈。 Python 决定崩溃并燃烧并发明一个全新的 python (python2k -> python3k) 来尝试解决这个问题,这会显示出来。天啊,真是太痛苦了,他们为了解决这个疏忽而受苦了。
but I cannot get the output of zipOut to write to a string and then have the string write to a file and then via the OS open the zipfile. How can that be done?
因此,用 'byte array' 替换该句子中所有出现的 'string',一切都很好!
这正是我要找的:
public void zip() {
try {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ZipOutputStream zipOut = new ZipOutputStream(baos);
ByteArrayInputStream bais = new ByteArrayInputStream(testString.getBytes());
System.out.println("testString " + testString + " testString.getBytes() " + testString.getBytes());
ZipEntry zipEntry = new ZipEntry("Results.xml");
zipOut.putNextEntry(zipEntry);
byte[] bytes = new byte[1024];
int length;
while((length = bais.read(bytes)) >= 0 ) {
zipOut.write(bytes, 0, length);
}
zipOut.close();
bais.close();
baos.close();
byte[] zipBytes = baos.toByteArray();
// just a test to see if can be opened in the operating system
OutputStream os = new FileOutputStream(new File("c:/t/again.zip"));
os.write(zipBytes);
os.close();
} catch(IOException e) {
e.printStackTrace();
}
}