我需要实施一个解决方案来创建受密码保护的 ZIP 流并将其保存到数据库作为
斑点。还需要从数据库中读取受密码保护的内容作为流读取。这个
不应创建物理文件。标准 JAVA SDK 不支持创建和读取密码
受保护的 ZIP。我尝试了不同的解决方案,所有大多数可用的解决方案都创建了一个物理文件。
我找到了使用 ZIP4J 写/读受密码保护的 ZIP 的示例
是否可以在不创建物理文件的情况下使用 ZIP4j 库创建和读取受密码保护的 ZIP。?
将补丁应用到其他可用源似乎很难满足我的要求。
我正在尝试在我的项目中使用 zip4j 来创建可下载的 zip 文件。该应用程序对我的朋友来说工作正常,但每当我尝试运行它时,我都会收到此错误:
java.lang.NoClassDefFoundError: net/lingala/zip4j/exception/ZipException
at java.lang.Class.getDeclaredMethods0(Native Method)
at java.lang.Class.privateGetDeclaredMethods(Class.java:2693)
at java.lang.Class.getDeclaredMethods(Class.java:1967)
at org.springframework.util.ReflectionUtils.getDeclaredMethods(ReflectionUtils.java:571)
at org.springframework.util.ReflectionUtils.doWithMethods(ReflectionUtils.java:488)
at org.springframework.util.ReflectionUtils.doWithMethods(ReflectionUtils.java:474)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.determineCandidateConstructors(AutowiredAnnotationBeanPostProcessor.java:241)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.determineConstructorsFromBeanPostProcessors(AbstractAutowireCapableBeanFactory.java:1065)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1038)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:504)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:476)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:303)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:299)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:755)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:757)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:480)
at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:403)
at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:306)
at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:106)
at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4772)
at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5196)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:725)
at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:701)
at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:714)
at org.apache.catalina.startup.HostConfig.manageApp(HostConfig.java:1585)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:483)
at org.apache.tomcat.util.modeler.BaseModelMBean.invoke(BaseModelMBean.java:300)
at com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.invoke(DefaultMBeanServerInterceptor.java:819)
at com.sun.jmx.mbeanserver.JmxMBeanServer.invoke(JmxMBeanServer.java:801)
at …Run Code Online (Sandbox Code Playgroud) 我有一个数据库,其中包含 blob 和该数据库内受密码保护的 zip,使用我传统上看到的标准文件对象方法
File zipFile = new File("C:\\file.zip");
net.lingala.zip4j.core.ZipFile table = new net.lingala.zip4j.core.ZipFile(zipFile);
if (table.isEncrypted())
table.setPassword(password);
net.lingala.zip4j.model.FileHeader entry = table.getFileHeader("file_inside_the_zip.txt");
return table.getInputStream(entry); //Decrypted inputsteam!
Run Code Online (Sandbox Code Playgroud)
我的问题是,我如何在不使用临时文件的情况下实现这样的东西,并且纯粹单独获取 blob 的输入流,到目前为止我有这样的东西
InputStream zipStream = getFileFromDataBase("stuff.zip");
//This point forward I have to save zipStream as a temporary file and use the traditional code above
Run Code Online (Sandbox Code Playgroud) 以下代码,如何使用java将文件添加到zip:
String source = "C:/Users/XXXXX/Desktop/Helicopter.zip";
try {
ZipFile zipFile = new ZipFile(source);
ZipParameters parameters = new ZipParameters();
zipFile.addFile(new File("C:/Users/XXXXXX/Desktop/HELLO_HELICOPTER.txt"), parameters);
} catch (net.lingala.zip4j.exception.ZipException e) {
e.printStackTrace();
}
Run Code Online (Sandbox Code Playgroud)
我如何能够将文件添加到此 zip 存档的特定文件夹中?
我在android中使用zip4j库生成zipfile时遇到问题,代码如下:
try {
ZipFile zipFile = new ZipFile(dest);
zipFile.setFileNameCharset("GBK");
if (srcFile.isDirectory()) {
zipFile.addFolder(srcFile, parameters);
} else {
zipFile.addFile(srcFile, parameters);
}
} catch (Exception e) {
e.printStackTrace();
}
Run Code Online (Sandbox Code Playgroud)
我得到了异常消息:
net.lingala.zip4j.exception.ZipException: Probably not a zip file or a corrupted zip file
at net.lingala.zip4j.core.HeaderReader.readEndOfCentralDirectoryRecord(HeaderReader.java:179)
at net.lingala.zip4j.core.HeaderReader.readAllHeaders(HeaderReader.java:78)
at net.lingala.zip4j.core.ZipFile.readZipInfo(ZipFile.java:425)
at net.lingala.zip4j.core.ZipFile.checkZipModel(ZipFile.java:935)
at net.lingala.zip4j.core.ZipFile.addFiles(ZipFile.java:263)
at net.lingala.zip4j.core.ZipFile.addFile(ZipFile.java:250)
Caused by: java.io.IOException: Negative seek offset
at java.io.RandomAccessFile.seek(RandomAccessFile.java:555)
at net.lingala.zip4j.core.HeaderReader.readEndOfCentralDirectoryRecord(HeaderReader.java:117)
Run Code Online (Sandbox Code Playgroud)
这段代码在 Eclipse 中非常适合本地文件,但在 Android 中效果不佳,我很确定目标 .zip 路径是正确的,并且 zip4j 库版本是 1.3.2。
谁能给我任何建议?