在BlackBerry Java应用程序中压缩文件
有人知道如何使用ZipoutStream压缩文件吗在BlackBerry Java应用程序中压缩文件,java,blackberry,java-me,Java,Blackberry,Java Me,有人知道如何使用ZipoutStream压缩文件吗 数据应写入ZipoutStream zinstream,而不是写入新的OutputStream out 书写完成后关闭ZipEntry条目也很重要 FileConnection path = (FileConnection) Connector.open( "file:///SDCard/BlackBerry/documents/" + "status.zip", Connector.READ_WRITE);
数据应写入ZipoutStream zinstream,而不是写入新的OutputStream out 书写完成后关闭ZipEntry条目也很重要
FileConnection path = (FileConnection) Connector.open(
"file:///SDCard/BlackBerry/documents/" + "status.zip",
Connector.READ_WRITE);
if (!path.exists()) {
path.create();
}
ZipOutputStream zinstream = new ZipOutputStream(path.openOutputStream());
// Adding Entries
FileConnection jsonfile = (FileConnection) Connector.open(
"file:///SDCard/BlackBerry/documents/" + "status.json",
Connector.READ_WRITE);
if (!jsonfile.exists()) {
jsonfile.create();
}
int fileSize = (int) jsonfile.fileSize();
if (fileSize > -1) {
InputStream input = jsonfile.openInputStream();
byte[] data = new byte[1024];
ZipEntry entry = new ZipEntry(jsonfile.getName());
zinstream.putNextEntry(entry);
int len;
while ((len = input.read(data)) > 0) {
zinstream.write(data, 0, len);
}
zinstream.closeEntry();
}
jsonfile.close();
zinstream.close();
path.close();
BlackBerry使用J2MEAPI,它没有所有的J2SE类,比如ZipoutStream和ZipEntry以及相关的类。有些类,如ZLibOutputStream,可能会有所帮助,但这只是字节级压缩,除非有第三方库可以为您实现,否则您将不得不自己实现实际的PKZIP容器。您会遇到什么错误?我已经下载了用于此任务的ZipME。谢谢你的建议!非常感谢你。它现在工作得很好。我没有意识到输出应该是ZipOutputStream而不是OutputStream
FileConnection path = (FileConnection) Connector.open(
"file:///SDCard/BlackBerry/documents/" + "status.zip",
Connector.READ_WRITE);
if (!path.exists()) {
path.create();
}
ZipOutputStream zinstream = new ZipOutputStream(path.openOutputStream());
// Adding Entries
FileConnection jsonfile = (FileConnection) Connector.open(
"file:///SDCard/BlackBerry/documents/" + "status.json",
Connector.READ_WRITE);
if (!jsonfile.exists()) {
jsonfile.create();
}
int fileSize = (int) jsonfile.fileSize();
if (fileSize > -1) {
InputStream input = jsonfile.openInputStream();
byte[] data = new byte[1024];
ZipEntry entry = new ZipEntry(jsonfile.getName());
zinstream.putNextEntry(entry);
int len;
while ((len = input.read(data)) > 0) {
zinstream.write(data, 0, len);
}
zinstream.closeEntry();
}
jsonfile.close();
zinstream.close();
path.close();