提交 9cbeb086 编写于 作者: K kohsuke

added one more convenience method

git-svn-id: https://hudson.dev.java.net/svn/hudson/trunk/hudson/main@15170 71c3de6d-444a-0410-be80-ed276b4c234a
上级 48103818
...@@ -34,6 +34,7 @@ import hudson.remoting.Future; ...@@ -34,6 +34,7 @@ import hudson.remoting.Future;
import hudson.remoting.Pipe; import hudson.remoting.Pipe;
import hudson.remoting.RemoteOutputStream; import hudson.remoting.RemoteOutputStream;
import hudson.remoting.VirtualChannel; import hudson.remoting.VirtualChannel;
import hudson.remoting.RemoteInputStream;
import hudson.util.FormFieldValidator; import hudson.util.FormFieldValidator;
import hudson.util.IOException2; import hudson.util.IOException2;
import hudson.util.StreamResource; import hudson.util.StreamResource;
...@@ -316,41 +317,62 @@ public final class FilePath implements Serializable { ...@@ -316,41 +317,62 @@ public final class FilePath implements Serializable {
* Target directory to expand files to. All the necessary directories will be created. * Target directory to expand files to. All the necessary directories will be created.
* @since 1.248 * @since 1.248
*/ */
public void unzip(FilePath target) throws IOException, InterruptedException { public void unzip(final FilePath target) throws IOException, InterruptedException {
target.act(new FileCallable<Void>() { target.act(new FileCallable<Void>() {
public Void invoke(File dir, VirtualChannel channel) throws IOException { public Void invoke(File dir, VirtualChannel channel) throws IOException {
dir = dir.getAbsoluteFile(); // without absolutization, getParentFile below seems to fail unzip(dir,FilePath.this.read());
ZipInputStream zip = new ZipInputStream(FilePath.this.read());
java.util.zip.ZipEntry e;
try {
while((e=zip.getNextEntry())!=null) {
File f = new File(dir,e.getName());
if(e.isDirectory()) {
f.mkdirs();
} else {
File p = f.getParentFile();
if(p!=null) p.mkdirs();
FileOutputStream out = new FileOutputStream(f);
try {
IOUtils.copy(zip, out);
} finally {
out.close();
}
f.setLastModified(e.getTime());
zip.closeEntry();
}
}
} finally {
zip.close();
}
return null; return null;
} }
private static final long serialVersionUID = 1L;
});
}
/**
* Reads the given InputStream as a zip file and extracts it into this directory.
*
* @param _in
* The stream will be closed by this method after it's fully read.
* @since 1.283
*/
public void unzipFrom(InputStream _in) throws IOException, InterruptedException {
final InputStream in = new RemoteInputStream(_in);
act(new FileCallable<Void>() {
public Void invoke(File dir, VirtualChannel channel) throws IOException {
unzip(dir, in);
return null;
}
private static final long serialVersionUID = 1L; private static final long serialVersionUID = 1L;
}); });
} }
private void unzip(File dir, InputStream in) throws IOException {
dir = dir.getAbsoluteFile(); // without absolutization, getParentFile below seems to fail
ZipInputStream zip = new ZipInputStream(in);
java.util.zip.ZipEntry e;
try {
while((e=zip.getNextEntry())!=null) {
File f = new File(dir,e.getName());
if(e.isDirectory()) {
f.mkdirs();
} else {
File p = f.getParentFile();
if(p!=null) p.mkdirs();
FileOutputStream out = new FileOutputStream(f);
try {
IOUtils.copy(zip, out);
} finally {
out.close();
}
f.setLastModified(e.getTime());
zip.closeEntry();
}
}
} finally {
zip.close();
}
}
/** /**
* Place the data from {@link FileItem} into the file location specified by this {@link FilePath} object. * Place the data from {@link FileItem} into the file location specified by this {@link FilePath} object.
*/ */
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册