Solve problems with file paths in the compression package

xiaoxiao2021-03-06  64

If you need to reprint, please indicate the source and the author. Thank you QQ: 221704msn: flyly@yeah.netemail: zhangfl@sports.cn

Today, use java.util.zip to make file compression. After writing the program, I found that the compressed package brought into all path directories of the original file.

/ ** * Generate zip file * Return to zip file address * @Param imgids string [] * @Return string * / public string createzip (string [] imgids) {string [] filenames = getPath (IMGIDS); // According to image ID return path Date date = new Date (); byte [] buf = new byte [1024]; String outFilename = zippath String.valueOf (date.getTime ()) ". zip"; try {ZipOutputStream out = new ZipOutputStream ( new FileOutputStream (outFilename)); for (int i = 0; i 0) {Out.write (BUF, 0, LEN);} out.closeentry (); in . close ();} catch (ooException ex) {ex.printstacktrace ();} return outfilename

In fact, the problem appears in zipentry zipentity = new zipentry (filenames [i]); I discovered this file name with full path after I Println this filename [i].

Solution

Public string createzip (string [] imgids) {string [] filenames = getPath (imgids); Date Date = new Date (); byte [] buf = new byte [1024]; string outfileename = zippath string.valueof (date. getTime ()) ". zip"; try {ZipOutputStream out = new ZipOutputStream (new FileOutputStream (outFilename)); for (int i = 0; i 0) {Out.write (buf, 0, len);} out.closeentry (); in .close (); Out.close ();} catch (ooException ex) {ex.printstacktrace ();} return outfilename;} Some details are not noticed. resulting in such a simple issue.

转载请注明原文地址:https://www.9cbs.com/read-88775.html

New Post(0)