Spring: how to parse uploaded zip file?

瘦欲@ 提交于 2019-12-12 20:08:05

问题


I uploaded my zip archive to the server and want to open .txt and .jpg files in it. I successfully get my archive in my Controller and get the name of each file via ZipEntry. Now I want to open it but for this I should get a full path to my file.

I haven't found how I can do that. Could you suggest some approach how to do that ?

Update

I try to use example have been suggested below but I am not be able open the file

ZipFile zFile = new ZipFile("trainingDefaultApp.zip");

I have got the FileNotFoundException

So I return to my start point. I have upload form in Java Spring application. In controller I had got a zip archive as byte[]

@RequestMapping(method = RequestMethod.POST)
public String create(UploadItem uploadItem, BindingResult bindingResult){
    try {
        byte[] zip = uploadItem.getFileData().getBytes();
        saveFile(zip);

Then I had got each ZipEntry

    InputStream is = new ByteArrayInputStream(zip);
    ZipInputStream zis = new ZipInputStream(is);

    ZipEntry entry = null;
    while ((entry = zis.getNextEntry()) != null) {
        String entryName = entry.getName();
        if (entryName.equals("readme.txt")) {
            ZipFile zip = new ZipFile(entry.getName()); // here I had got an exception

According to docs I did all right but as for me it is strange to pass the file name only and suspect that you successfully will open the file


回答1:


zipFile.getInputStream(ZipEntry entry) will return you the inputstream for the specific entry.

Check out the javadocs for ZipFile.getInputStream() - http://docs.oracle.com/javase/6/docs/api/java/util/zip/ZipFile.html#getInputStream(java.util.zip.ZipEntry).

Update:

I misread your question. For using the ZipInputStream, there is sample code on Oracle's website (http://java.sun.com/developer/technicalArticles/Programming/compression/) that shows you how to read from the stream. See the first code sample: Code

  • Sample 1: UnZip.java.

Copying here, it is reading from the entry and writing it directly to a file, but you could replace that with whatever logic you need:

ZipInputStream zis = new ZipInputStream(new BufferedInputStream(fis));
ZipEntry entry;
while((entry = zis.getNextEntry()) != null) {
   System.out.println("Extracting: " +entry);
   int count;
   byte data[] = new byte[BUFFER];
   // write the files to the disk
   FileOutputStream fos = new FileOutputStream(entry.getName());
   dest = new 
   BufferedOutputStream(fos, BUFFER);

   while ((count = zis.read(data, 0, BUFFER)) != -1) {
        dest.write(data, 0, count);
   }
}



回答2:


I resolve my uissue. The solution is work directly with ZipInputStream. Here the code:

    private void saveFile(byte[] zip, String name, String description) throws IOException {
    InputStream is = new ByteArrayInputStream(zip);
    ZipInputStream zis = new ZipInputStream(is);

    Application app = new Application();
    ZipEntry entry = null;
    while ((entry = zis.getNextEntry()) != null) {
        String entryName = entry.getName();
        if (entryName.equals("readme.txt")) { 
           new Scanner(zis); //!!!
           //... 
           zis.closeEntry();


来源:https://stackoverflow.com/questions/11016158/spring-how-to-parse-uploaded-zip-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!