将文件从一个zipfile子文件夹复制到其他zipfile子文件夹。 version
和folder
是从前端获取的子文件夹名称。
public String restore(String jobId, String version, String folder) {
String fileName = String.valueOf(jobId) + ".zip";
String versions[] = version.replaceAll("'", "")
.replace("[", "")
.replace("]", "")
.split(",");
String folders[] = folder.replaceAll("'", "").replace("[", "").replace("]", "").split(",");
ArrayList<String> listofVersion, listofFolder = new ArrayList<>();
File destinationFile = new File(env.getProperty("file.path") + fileName);
File sourceFile = new File(env.getProperty("file.arcivePath") + fileName);
FileInputStream in;
FileOutputStream out;
ZipInputStream zin;
ZipOutputStream zipout;
int BUFFER = (int) sourceFile.length();
if (!destinationFile.exists()) {
try {
out = new FileOutputStream(destinationFile);
zipout = new ZipOutputStream(out); //I get the "out" object from the servlet which tells the content type and and final zip file location.
ZipEntry zEntry, ze;
in = new FileInputStream(sourceFile);
zin = new ZipInputStream(in);
while ((zEntry = zin.getNextEntry()) != null) {
System.out.println(zEntry);
String entry = zEntry.getName();
String entryList[] = entry.split("/");
if (zEntry.isDirectory() && (entryList.length <= 1)) {
zipout.putNextEntry(zEntry);
} else {
if (entryList.length >= 2) {
listofVersion = listVersionRestore(fileName);
for (int j = 0; j < versions.length; j++) {
for (int i = 0; i < listofVersion.size(); i++) {
if (listofVersion.get(i).equals(versions[j])) {
if ((entryList.length == 2) && entryList[1].equals(versions[j])) {
zipout.putNextEntry(zEntry);
}
if (entryList.length >= 3) {
listofFolder = listFolderRestore(fileName, versions[j]);
for (int l = 0; l < folders.length; l++) {
for (int k = 0; k < listofFolder.size(); k++) {
if (listofFolder.get(k).equals(folders[l])) {
if ((entryList.length == 3) && entryList[1].equals(versions[j]) && entryList[2].equals(folders[l])) {
zipout.putNextEntry(zEntry);
}
if ((entryList.length == 4) && entryList[1].equals(versions[j]) && entryList[2].equals(folders[l])) {
zipout.putNextEntry(zEntry);
byte[] buffer = new byte[BUFFER];
int len = 0;
while ((len = zin.read(buffer)) != -1) {
zipout.write(buffer, 0, len);
}
zipout.closeEntry();
in.close();
}
}
}
}
}
}
}
}
}
}
}
zipout.close();
} catch (IOException e) {
e.printStackTrace();
return "Error";
}
} else {
return "Already Exists";
}
return "RestoredSuccessfully";
}
获得如下错误:
java.util.zip.ZipException: invalid entry compressed size (expected 449 but got 455 bytes)
at java.util.zip.ZipOutputStream.closeEntry(Unknown Source)
at prj.iopo.filesystem.FileSystemHandler.restore(FileSystemHandler.java:148)
at prj.iopo.filesystem.FileSystemHandler$$FastClassBySpringCGLIB$$f51baf3f.invoke(<generated>)
at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204)
at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:720)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157)
at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:136)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:136)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:655)
at prj.iopo.filesystem.FileSystemHandler$$EnhancerBySpringCGLIB$$6d76d700.restore(<generated>)
at prj.iopo.controller.JobController.restore(JobController.java:99)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:222)
at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:137)
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:110)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:814)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:737)
at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:959)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:893)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:969)
at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:860)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:622)
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:845)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:291)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:316)
at org.springframework.security.web.access.intercept.FilterSecurityInterceptor.invoke(FilterSecurityInterceptor.java:126)
at org.springframework.security.web.access.intercept.FilterSecurityInterceptor.doFilter(FilterSecurityInterceptor.java:90)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:330)
at org.springframework.security.web.access.ExceptionTranslationFilter.doFilter(ExceptionTranslationFilter.java:114)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:330)
at org.springframework.security.web.session.SessionManagementFilter.doFilter(SessionManagementFilter.java:122)89765042/1/2l/FS-restore.txt
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:330)
at org.springframework.security.web.authentication.AnonymousAuthenticationFilter.doFilter(AnonymousAuthenticationFilter.java:111)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:330)
at org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter.doFilter(SecurityContextHolderAwareRequestFilter.java:169)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:330)
at org.springframework.security.web.savedrequest.RequestCacheAwareFilter.doFilter(RequestCacheAwareFilter.java:48)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:330)
at org.springframework.security.web.authentication.www.BasicAuthenticationFilter.doFilterInternal(BasicAuthenticationFilter.java:213)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:330)
at org.springframework.security.web.authentication.logout.LogoutFilter.doFilter(LogoutFilter.java:120)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:330)
at org.springframework.security.web.header.HeaderWriterFilter.doFilterInternal(HeaderWriterFilter.java:64)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:330)
at org.springframework.security.web.context.SecurityContextPersistenceFilter.doFilter(SecurityContextPersistenceFilter.java:91)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:330)
at org.springframework.security.web.context.request.async.WebAsyncManagerIntegrationFilter.doFilterInternal(WebAsyncManagerIntegrationFilter.java:53)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:330)
at org.springframework.security.web.FilterChainProxy.doFilterInternal(FilterChainProxy.java:213)
at org.springframework.security.web.FilterChainProxy.doFilter(FilterChainProxy.java:176)
at org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:346)
at org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:262)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:99)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.springframework.web.filter.HttpPutFormContentFilter.doFilterInternal(HttpPutFormContentFilter.java:87)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.springframework.web.filter.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:77)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:121)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:212)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:141)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:521)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1096)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:674)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1500)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1456)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Unknown Source)
我在zipout.closeEntry();
收到错误
但我无法删除此行。
答案 0 :(得分:4)
使用新zipentry的新流解决了错误。
loc_ze = new ZipEntry(zEntry.getName());
zipout.putNextEntry(loc_ze);
zipfile = new ZipFile(sourceFile);
InputStream stream = zipfile.getInputStream(zEntry);
while ((len = stream.read(b)) != -1) {
zipout.write(b, 0, len);
}
答案 1 :(得分:2)
您需要创建一个新的RewriteEngine On
RewriteCond %{HTTP_HOST} ^www.name.com [NC]
RewriteRule ^(.*)$ http://name.com/$1 [L,R=301]
AddDefaultCharset UTF-8
RewriteRule ^/(css|js|img)/(.*)?$ /$1/$2 [L,QSA,R=301]
RewriteRule ^([a-zA-Z0-9-/]+)/([a-zA-Z0-9-/]+)/?$ detail.php?title=$1&id=$2
RewriteRule ^([a-zA-Z0-9-/]+)/?$ detail.php?id=$1
Options -Indexes
对象以放入ZIP输出流。您不能重复使用ZIP输入流中的一个:它包含ZIP输出流想要为自己确定的内容。使用适当的构造函数从源ZEntry
复制真正需要的内容。
答案 2 :(得分:0)
也许最简单的解决方案是 TrueZip (https://truezip.java.net/),请参阅此帖子Appending files to a zip file with Java
EJP是对的。要复制同一存档中的文件,您需要两个ZipEntry对象,因为每个ZipEntry
对象代表zip存档中的一个文件或目录
请参阅此主题Reading text files in a zip archive和此链接http://www.java2s.com/Code/Java/File-Input-Output/ReadingtheContentsofaZIPFile.htm
您需要一个ZipFile
对象,您可以将zip文件的位置路径传递给
ZipFile zf = new ZipFile("/<path>/yourZip.zip");
然后创建ZipFile对象的所有元素的枚举
Enumeration entries = zf.entries();
如果要复制存档中的文件,可以跳过此步骤。
[>要读取条目的内容,请使用包装到BufferedReader中的InputStream(二进制到ASCII)(读取文本文件,格式字符如EOL或EOF)BufferedReader br = new BufferedReader(new InputStreamReader(input, "UTF-8"));
] < /强>
另一种方法是ZipInputStream
/ ZipOutputStream
以下代码读取zip存档(http://www.java2s.com/Code/Java/File-Input-Output/ReadingtheContentsofaZIPFile.htm)的内容:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Enumeration;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
public class ReadZip {
public static void main(String args[]) {
try {
ZipFile zf = new ZipFile("ReadZip.zip");
Enumeration entries = zf.entries();
BufferedReader input = new BufferedReader(new InputStreamReader(
System.in));
while (entries.hasMoreElements()) {
ZipEntry ze = (ZipEntry) entries.nextElement();
System.out.println("Read " + ze.getName() + "?");
String inputLine = input.readLine();
if (inputLine.equalsIgnoreCase("yes")) {
long size = ze.getSize();
if (size > 0) {
System.out.println("Length is " + size);
BufferedReader br = new BufferedReader(
new InputStreamReader(zf.getInputStream(ze)));
String line;
while ((line = br.readLine()) != null) {
System.out.println(line);
}
br.close();
}
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
答案 3 :(得分:0)
static copyZip() throws IOException {
try (final ZipInputStream zis = new ZipInputStream(...);
final ZipOutputStream zos = new ZipOutputStream(...)
) {
copyZipContent(zis, zos);
}
}
static void copyZipContent(final ZipInputStream zis, final ZipOutputStream zos) throws IOException {
ZipEntry zisEntry;
while ((zisEntry = zis.getNextEntry()) != null) {
ZipEntry zosEntry = new ZipEntry(zisEntry.getName());
zosEntry.setComment(zisEntry.getComment());
zosEntry.setExtra(zisEntry.getExtra());
zos.putNextEntry(zosEntry);
IOUtils.copy(zis, zos);
zos.closeEntry();
}
zos.finish();
}