Générer un zip contenant les documents déposés

J’ai un objet métier myObjPj qui contient les pj déposées sur des dossiers dans un field myObjPjDoc
est-il possible de générer un zip contenant toutes les pj des dossiers sélectionnés dans une liste ?
le pj peuvent être de tous types (pdf, word, excel, …)

C’est une fonction standard de la V5.1.

On peut exporter une liste en ZIP avec les pièces jointes :

  • le ficher xlsx des lignes à la racine
  • un répertoire par champ document (nommé par sa traduction)
  • les PJ dans le répertoire, en cas de conflit de fichiers même nom, un index est ajouté comme sous windows : “fichier.pdf”, “fichier (1).pdf”, “fichier (2).pdf”

En V4 ou pour tout autre besoin de ZIP spécifique, il faudra fabriquer le ZIP via un objet externe (appelé par un bouton d’action ou une URL) qui retourne le ZIP.

Extrait de l’export en V5.1 à réadapter, l’OutputStream pouvant être un ByteArrayOutputStream, un FileOutputStream en vu de stocker le ZIP quelque part, ou directement l’output de la réponse HTTP pour ne consommer aucune mémoire serveur :

public static String toARC(ObjectDB obj, boolean list, String mode, HttpServletResponse response)
{
	try (OutputStream out = response.getOutputStream())
	{
		return toARC(obj, list, mode, out);
	}
	catch (IOException e)
	{
		return "Unable to build zip: " + e.getMessage();
	}
}

public static String toARC(ObjectDB obj, boolean list, String mode, OutputStream out)
{
	String err = null;
	String tmpdir = FileTool.getRandomDirname(Platform.getTmpDir(), "arc");
	boolean full = ObjectXML.MODE_FULL.equals(mode);

	try
	{
		// Prepare tree in temporary directory
		File tmp = new File(tmpdir);
		tmp.mkdirs();
		List<String> files = new ArrayList<>();

		// Excel file
		boolean poi = Globals.poiLibAvailable();
		String file = obj.getName()+"."+(poi ? "xlsx" : "xls");
		try (FileOutputStream fos = new FileOutputStream(tmpdir+"/"+file))
		{
			ExcelTool tool = new ExcelTool();
			if (list)
				tool.export(obj, null, mode,  Globals.getDefaultExcelFormat(), fos);
			else // form
				tool.export(obj, mode, Globals.getDefaultExcelFormat(), fos);
		}
		files.add(file);

		// Attachments
		if (list)
		{
			int maxRows = 1000;
			obj.searchExport(true, maxRows, (page) -> {
				for (String[] row : page)
				{
					obj.setValues(row, false);
					toARCdocs(obj, full, tmpdir, true, files);
				}
			});
		}
		else // single row
		{
			toARCdocs(obj, full, tmpdir, false, files);
		}

		// Build ZIP
		File fz = ZIPTool.build(tmpdir, obj.getName()+".zip", files);
		// Copy in out and discard
		try (DeleteOnCloseInputStream in = new DeleteOnCloseInputStream(fz))
		{
			Tool.copy(in, out);
		}
	}
	catch (Exception e)
	{
		err = "Unable to build zip: " + e.getMessage();
		AppLog.log("ECORED0001", ObjectImportExport.class, "exportARC", "Unable to build zip", e);
	}
	finally
	{
		// Purge temporary directory
		File tmp = new File(tmpdir);
		if (tmp.isDirectory())
			FileTool.deleteFileOrDir(tmp, true);
	}
	return err;
}

private static void toARCdocs(ObjectDB obj, boolean full, String tmpdir, boolean list, List<String> files)
{
	for (ObjectField f : obj.getDocFields())
	{
		if (list && !CSVTool.isListVisible(full, obj, f))
			continue;
		else if (!list && !CSVTool.isFormVisible(full, obj, f))
			continue;

		DocumentDB doc = f.getDocument(obj.getGrant());
		if (doc==null)
			continue;

		String path = doc.getPath();
		if (!Tool.isEmpty(path))
		{
			String docName = path.substring(path.lastIndexOf('/')+1);
			String dstPath = f.getDisplay()+"/"+docName;
			File dst = new File(tmpdir+"/"+dstPath);

			// second chance with increment "name (n).ext"
			// to be unique in ZIP
			if (dst.exists())
			{
				int n = 0;
				int dot = docName.lastIndexOf(".");
				String name = dot<0 ? docName : docName.substring(0,dot);
				String ext  = dot<0 ? null : docName.substring(dot);
				do
				{
					n++;
					docName = name + " ("+n+")" + (ext!=null ? ext : "");
					dstPath = f.getDisplay()+"/"+docName;
					dst = new File(tmpdir+"/"+dstPath);
				}
				while (dst.exists());
			}
			if (!dst.exists())
			{
				doc.copyTo(dst.getAbsolutePath());
				files.add(dstPath);
			}
		}
	}
}

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.