Compress multiple pictures, unpacked found picture file corruption problem solving

Turn: https: //blog.csdn.net/liangpzhmz/article/details/75308732

Recent projects needs to be done in a function, it is a multiple pictures compressed zip file, I am using the java ZipOutputStream to achieve. Finally, the development is over, after tests found that only the first extract the image files can be opened, behind a few pictures are not open. Because there is no plus bos.flush ();

Package com.ck.common; 

Import the java.io. * ;
 Import the java.text.SimpleDateFormat;
 Import java.util.zip.ZipEntry;
 Import java.util.zip.ZipInputStream;
 Import java.util.zip.ZipOutputStream; 

/ * * 
 * DATE: 2020/01/27 
 * file compression and decompression tool class 
 * / 
public  class FileZip { 

    / ** 
     * ZIP file compression 
     * @param inputFile be compressed folder / file name 
     * @param outputFile generate archive name
      * / 
    public  static  void ZipCompress (inputFile String, String outputFile) throws{Exception
         // create zip output stream 
        the ZipOutputStream OUT = new new the ZipOutputStream ( new new a FileOutputStream (the outputFile));
         // create a buffered output stream 
        BufferedOutputStream The BOS = new new BufferedOutputStream The (OUT); 
        File INPUT = new new File (inputFile); 
        the compress (OUT, BOS , the INPUT, null ); 
        bos.close (); 
        out.close (); 
    } 

    / ** 
     * @param name the name of the compressed file, you can write to keep the default is null 
     * recursive compression 
     * / 
    public  static  voidthe compress (the ZipOutputStream OUT, BufferedOutputStream The BOS, File INPUT, String name) throws IOException {
         IF (name == null ) { 
            name = input.getName (); 
        } 
        // if the path to the directory (folder) 
        IF (input.isDirectory ( )) {
             // out files in the folder (or sub-folders) 
            file [] = flist input.listFiles (); 

            IF (flist.length == 0) // if the folder is empty, then the object simply writing to a zip file into the directory 
            { 
                out.putNextEntry ( new new the ZipEntry (name + the File.separator)); 
            } the else //If the folder is not empty, then the recursive call the compress, each folder in a file (or folder) is compressed 
            {
                 for ( int I = 0; I <flist.length; I ++ ) { 
                    the compress (OUT, BOS, flist [I], name + + the File.separator flist [I] .getName ()); 
                } 
            } 
        } the else // If not a directory (folder), is the document, the first point is written into the directory, write file after the zip file 
        { 
            out.putNextEntry ( new new the ZipEntry (name)); 
            the FileInputStream fos = new new the FileInputStream (INPUT); 
            BufferedInputStream BIS = new newBufferedInputStream (fos);
             int len = -1 ;
             // be written to the source file zip file 
            byte [] = buf new new  byte [1024 ];
             the while (! (Len = bis.read (buf)) = -1 ) { 
                bos.write (buf, 0 , len); 
            } bos.flush (); 
            bis.close (); 
            fos.close (); 
        } 
    } 
           

    / ** 
     *-extracting ZIP 
     * @param inputFile be decompressed file name 
     * @param destDirPath extraction path
      * / 
    public  static  voidZipUncompress (inputFile String, String destDirPath) throws Exception { 
        File srcFile = new new File (inputFile); // get the current archive
         // determine whether the source file exists 
        IF (! {SrcFile.exists ())
             the throw  new new Exception (srcFile.getPath () + "file does not exist within the meaning of" ); 
        } 
        // start extracting
         // Construction decompressed input stream 
        ZipInputStream Zin = new new ZipInputStream ( new new the FileInputStream (srcFile)); 
        the ZipEntry entry = null ; 
        file file = null;
        while ((entry = zIn.getNextEntry()) != null) {
            if (!entry.isDirectory()) {
                file = new File(destDirPath, entry.getName());
                if (!file.exists()) {
                    new File(file.getParent()).mkdirs();//创建此文件的上级目录
                }
                OutputStream out = new FileOutputStream(file);
                BufferedOutputStream bos = new BufferedOutputStream(out);
                int len = -1;
                 Byte [] = buf new new  byte [1024 ];
                 the while ((len = zIn.read (buf)) = -1! ) { 
                    Bos.write (buf, 0 , len); 
                } 
                // off stream order, first open after closing 
                bos.close (); 
                the out.close (); 
            } 
        } 
    } 

    public  static  void main (String [] args) {
         the try {
 //             the SimpleDateFormat new new DF = the SimpleDateFormat ( "yyyyMMddHHmmss"); // set the date format
 / /            System.out.println (df.format (new new a Date ())); // new new a Date () to acquire the current system time 

        ZipCompress ( "F: \\ SDF", "E: \\ \\ ss.zip file1" ); 
          ZipUncompress ( "E: \\ \\ ss.zip file1", "E: \\ file1" ); 
        } the catch (Exception E) { 
           e.printStackTrace (); 
        } 
    } 

}

Guess you like

Origin www.cnblogs.com/nb123/p/12166716.html