Package com.ffcs.icity.common.util;
Import Java.io.ByteArrayInputStream;
Import Java.io.File;
Import Java.io.FileOutputStream;
Import java.io.IOException;
Import Java.io.InputStream;
Import Java.util.HashMap;
Import Java.util.Map;
Import Java.util.zip.ZipEntry;
Import Java.util.zip.ZipInputStream;
Import Org.apache.commons.io.FilenameUtils;
Import Org.apache.commons.io.IOUtils;
Import Org.apache.commons.lang3.RandomStringUtils;
Import Org.slf4j.Logger;
Import Org.slf4j.LoggerFactory; /** * Provides a compression decompression for the file of the Operation tool class * @author Linwei * * */public class Ziputil {private final static Logger log = Loggerfactor
Y.getlogger (Ziputil.class); /** * Perform decompression of compressed files and automatically generate file operations * @param in * @param outputdirectory * * * @return */public static Map<string,o
Bject> UnZip (InputStream in,string outputdirectory) {if (in = = NULL) return null;
ZipEntry zipentry = null;
FileOutputStream out = null;
String uniquename,iconurl = null; map<string,object> map = new HashMap<string,object> ();
Zipinputstream Zipin = new Zipinputstream (in);
try{while ((ZipEntry = Zipin.getnextentry ())!= null) {//If the folder path is the way, the operation if (Zipentry.isdirectory ()) is temporarily not provided in this method {
String name = Zipentry.getname ();
Name = name.substring (0, Name.length ()-1);
File File = new file (outputdirectory + file.separator + name);
File.mkdir ();
else {//If it is a file, generate UniqueName = Getsavedfilename (Zipentry.getname ()) directly under the corresponding path;
File path = new file (outputdirectory + file.separator);
if (!path.exists ()) path.mkdirs ();
Iconurl = outputdirectory + File.separator + uniquename;
File File = new file (Iconurl);
File.createnewfile ();
out = new FileOutputStream (file);
int b = 0;
while ((b = zipin.read ())!=-1) {out.write (b);
} out.close ();
Map.put (Zipentry.getname (), iconurl);
} return map; The catch (Exception ex) {Log.error ("in UnZip" (InputStream in,stringOutputDirectory) has a error,e is "+ ex);
return null;
} finally{ioutils.closequietly (Zipin);
Ioutils.closequietly (in);
ioutils.closequietly (out);
/** * Unzip the compressed file stream, and based on the level of decompression to determine whether to resolve only the first layer, or parse all the data * map<name,inputstream> * @param in * @param parselevel * @return */public static map<string,object> UnZip (InputStream in,int parselevel) {if (in = = null) retu
RN null;
ZipEntry zipentry = null;
FileOutputStream out = null;
map<string,object> map = new hashmap<string,object> ();
Zipinputstream Zipin = new Zipinputstream (in);
try{while ((ZipEntry = Zipin.getnextentry ())!= null) {//If the folder path is the way, the operation if (Zipentry.isdirectory ()) is temporarily not provided in this method {
String name = Zipentry.getname ();
Name = name.substring (0, Name.length ()-1);
File File = new file (outputdirectory + file.separator + name);
File.mkdir ();
else {//If it is a file, it is stored directly in the map String name = Zipentry.getname (); Put pressureThe flow in the indented file into a byte array, enough external logic to use (after closing the stream) byte[] bt = Ioutils.tobytearray (Zipin);
Map.put (NAME,BT);
} return map;
The catch (Exception ex) {Log.error ("in UnZip (InputStream in,int parselevel) has a error,e is" + ex);
return null;
} finally{ioutils.closequietly (Zipin);
Ioutils.closequietly (in);
ioutils.closequietly (out); /** * Generates a unique new file name (with suffix) based on the file name passed (with suffix) * @param origfilename * @return/private static String Getsa Vedfilename (String origfilename) {return Datehelper.getcurrenttimestamp ("YYYYMMDDHHMMSS") + "-" +
Randomstringutils.randomnumeric (5) + "." + filenameutils.getextension (origfilename); public static void Main (string[] args) throws IOException {//FileInputStream in = new FileInputStream (New Fi
Le ("d:\\temp\\temp.zip"));
Map map = UnZip (in,0);
SYSTEM.OUT.PRINTLN ("map is" + map.size ());
byte[] bt = (byte[]) map.get ("Ziptest.zip");
byte[] bt = NULL; InputStream in1 = new BytearrayinputstreAM (BT);
UnZip (in1, "d://test");
System.err.println ("over.");
}
}