java zip文件夹,然后删除原始文件夹

时间:2018-09-20 18:37:20

标签: java zip delete-file

我有一个将包含三个“ .txt”文件的文件夹“ UNO”压缩成名为“ UNO.zip”的zip文件的类,并且我还有另一个递归地删除原始文件夹“ UNO”的类,并且所有内容。当我分别执行它们时,它们就像是一种魅力。但是,当我尝试在同一线程中执行它们时,压缩效果很好,但是删除部分只是删除了文件夹中的最后一个“ .txt”文件...而且我必须再运行两次该类,才能删除该类。 “ .txt”文件,最后是原始文件夹。

谁能告诉我为什么会这样? 谢谢。

这是代码:

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

public class Z3 {

    public static void main(String[] args) {

        byte[] buffer = new byte[1024];
        String ruta = "C:\\DESCARGAS\\";
        String nombreDeLaCarpeta = "UNO";
        List<String> nombresDeLosArchivos = new ArrayList<String>();

          File carpeta = new File( ruta + nombreDeLaCarpeta );
          File[] listadoEnCarpeta = carpeta.listFiles();
                if ( listadoEnCarpeta != null ) {
                    for ( int i = 0; i < listadoEnCarpeta.length; i ++ ) {

                        nombresDeLosArchivos.add( listadoEnCarpeta[i].getPath().substring( ruta.length() + nombreDeLaCarpeta.length() + 1, 
                                listadoEnCarpeta[i].getPath().length() ) );

                    }
               }

        FileOutputStream fos = null;
        ZipEntry ze = null;
        FileInputStream in = null;
        ZipOutputStream zos = null;

        try {
            fos = new FileOutputStream( "C:\\DESCARGAS\\UNO.zip" );
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }

        zos = new ZipOutputStream( fos );

        for ( int i = 0; i < nombresDeLosArchivos.size(); i ++ ) {
            ze = new ZipEntry( nombresDeLosArchivos.get(i) );
            try {
                zos.putNextEntry( ze );
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                in = new FileInputStream( ruta + nombreDeLaCarpeta + "\\" + nombresDeLosArchivos.get(i) );
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }

            int len;
            try {
                while ( ( len = in.read( buffer ) ) > 0 ) {
                    zos.write( buffer, 0, len );
                }
            } catch (IOException e) {
                e.printStackTrace();
            }

            }

            try {
                in.close();
                zos.closeEntry();
                zos.flush();
                zos.close();
                fos.flush();
                fos.close();

            } catch (IOException e) {
                e.printStackTrace();
            }

            // ------------------------------------------------------------

            File directory = new File( ruta + nombreDeLaCarpeta );
            if ( directory.exists() ) {
                    try {
                        eliminarSubcarpetas( directory );
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
            }


    }

    public static void eliminarSubcarpetas( File file ) throws IOException {

        File[] files = file.listFiles();
        if ( files != null ) {
            for ( File f: files ) {
                if ( f.isDirectory() ) {
                    eliminarSubcarpetas( f );
                } else {
                    f.delete();
                    System.out.println( f.getAbsolutePath() );
                }
            }
        }


      file.delete();

    }

}

1 个答案:

答案 0 :(得分:0)

一个简单的问题是输出流没有正确关闭。我只需要将压缩块放到另一种方法中就可以了。

代码如下:

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

public class Z2 {

    public static void main(String[] args) throws IOException  {

        List<String> nombresDeLosArchivos = new ArrayList<String>();

        File carpeta = new File( "C:\\DESCARGAS\\UNO" );
        File[] listadoEnCarpeta = carpeta.listFiles();

                if ( listadoEnCarpeta != null ) {
                    for ( int i = 0; i < listadoEnCarpeta.length; i ++ ) {

                        nombresDeLosArchivos.add( listadoEnCarpeta[i].getPath() );

                    }
               }

            zipArchivo( "C:\\DESCARGAS\\UNO.zip", nombresDeLosArchivos);

            File directory = new File( "C:\\DESCARGAS\\UNO" );
            if ( directory.exists() ) {

                    try {
                        eliminarSubcarpetas( directory );
                    } catch (IOException e) {
                        e.printStackTrace();
                    }   
            }


    }

    public static void zipArchivo( String zipFile, List<String> listaDeStrings ) throws IOException {

        byte[] buffer = new byte[1024];
        FileOutputStream fos = new FileOutputStream( zipFile );
        ZipOutputStream zos = new ZipOutputStream( fos );

        for ( int i = 0; i < listaDeStrings.size(); i ++ ) {

            File scrFile = new File( listaDeStrings.get(i) ); 
            FileInputStream fis = new FileInputStream ( scrFile );
            zos.putNextEntry( new ZipEntry ( scrFile.getName() ) );

            int length;

            while ( ( length = fis.read( buffer) ) > 0 ) {
                zos.write( buffer, 0, length );
            }

            zos.closeEntry();

            fis.close();

        }

        zos.close();


    }

    public static void eliminarSubcarpetas( File file ) throws IOException {

        File[] files = file.listFiles();
        if ( files != null ) {

            for ( File f: files ) {
                  if ( f.isDirectory() ) {
                    eliminarSubcarpetas( f );
                } else {
                    f.delete();
                }
            }
        }

        file.delete();

    }

}