How to extract a single file from a remote archive file?

前端 未结 4 1380
广开言路
广开言路 2020-12-05 20:29

Given

  1. URL of an archive (e.g. a zip file)
  2. Full name (including path) of a file inside that archive

I\'m looking for a way (preferably i

4条回答
  •  时光说笑
    2020-12-05 20:36

    I'm not sure if there's a way to pull out a single file from a ZIP without downloading the whole thing first. But, if you're the one hosting the ZIP file, you could create a Java servlet which reads the ZIP file and returns the requested file in the response:

    public class GetFileFromZIPServlet extends HttpServlet{
      @Override
      public void doGet(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException{
        String pathToFile = request.getParameter("pathToFile");
    
        byte fileBytes[];
        //get the bytes of the file from the ZIP
    
        //set the appropriate content type, maybe based on the file extension
        response.setContentType("...");
    
        //write file to the response
        response.getOutputStream().write(fileBytes);
      }
    }
    

提交回复
热议问题