I create an archive with the help of such a class, and when I deploy it, its structure consists of somewhere 10 folders (their names are repeated in chains of 4 - 5) and only in the latter are the archive files ... What am I doing wrong?

public class ZipUtil implements Runnable { private Context context; public ZipUtil(Context context) { this.context = context; } @Override public void run() { File zipFile = getZipFile(); ZipOutputStream out = null; try { out = new ZipOutputStream(new FileOutputStream(zipFile)); } catch (FileNotFoundException e) { System.out.println("ZipOutputStream not opened!!!!!!!!!!!!!1"); } try { //здесь мы отмечаем дирректорию которую мы хотим добавить в архив File dirToZip = ImageSaver.getAvatarPhotosDir(); doZip(dirToZip, out); } catch (IOException e) { e.printStackTrace(); } try { if (out != null) { out.close(); } } catch (IOException e) { e.printStackTrace(); } } private File getZipFile() { //здесь мы показываем где создать архив File dirForZip = ImageSaver.getAvatarPhotosDir().getParentFile(); String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date()); return new File(dirForZip + File.separator + "archive_" + timeStamp + ".zip"); } private static void doZip(File dir, ZipOutputStream out) throws IOException { System.out.println("ZIPing is started!!!!!!!!!!!!!!!!!!!!!!!!!!!!"); for (File tmp : dir.listFiles()) { if (tmp.isDirectory()) doZip(tmp, out); else { out.putNextEntry(new ZipEntry(tmp.getPath())); write(new FileInputStream(tmp), out); } } System.out.println("ZIPing finished!!!!!!!!!!!!!!!!!!!!!!!!!!!!"); } private static void write(InputStream in, OutputStream out) throws IOException { byte[] buffer = new byte[1024]; int len; while ((len = in.read(buffer)) >= 0) out.write(buffer, 0, len); in.close(); } } 

    1 answer 1

    In fact, the problem was that in the doZip () function in the string out.putNextEntry (new ZipEntry (tmp.getPath ())); you need to specify not tmp.getPath (), but tmp.getName (), since path duplicates the path and the code takes it as creating all the folders that are in the path of the file you specify ... Probably not quite clear, but in general If you have such a problem, then try to solve it in this way. It helped me.