{"record":{"id":"2b81ff7c99aad4ba","repo":"Kareadita/Kavita","slug":"generic-create-temp-archive","errorCode":null,"errorMessage":"generic-create-temp-archive","messagePattern":"generic-create-temp-archive","errorType":"exception","errorClass":"KavitaException","httpStatus":null,"severity":"error","filePath":"Kavita.Services/ArchiveService.cs","lineNumber":296,"sourceCode":"\n        directoryService.ExistOrCreate(tempLocation);\n\n        if (!directoryService.CopyFilesToDirectory(files, tempLocation))\n        {\n            throw new KavitaException(\"bad-copy-files-for-download\");\n        }\n\n        var zipPath = Path.Join(directoryService.TempDirectory, $\"kavita_{tempFolder}_{dateString}.zip\");\n        try\n        {\n            ZipFile.CreateFromDirectory(tempLocation, zipPath);\n            // Remove the folder as we have the zip\n            directoryService.ClearAndDeleteDirectory(tempLocation);\n        }\n        catch (AggregateException ex)\n        {\n            logger.LogError(ex, \"There was an issue creating temp archive\");\n            throw new KavitaException(\"generic-create-temp-archive\");\n        }\n\n        return zipPath;\n    }\n\n    public string CreateZipFromFoldersForDownload(IList<string> files, string tempFolder, Func<Tuple<string, float>, Task> progressCallback)\n    {\n        var dateString = DateTime.UtcNow.ToShortDateString().Replace(\"/\", \"_\");\n\n        var potentialExistingFile = directoryService.FileSystem.FileInfo.New(Path.Join(directoryService.TempDirectory, $\"kavita_{tempFolder}_{dateString}.cbz\"));\n        if (potentialExistingFile.Exists)\n        {\n            // A previous download exists, just return it immediately\n            return potentialExistingFile.FullName;\n        }\n\n        // Extract all the files to a temp directory and create zip on that\n        var tempLocation = Path.Join(directoryService.TempDirectory, $\"{tempFolder}_{dateString}\");","sourceCodeStart":278,"sourceCodeEnd":314,"githubUrl":"https://github.com/Kareadita/Kavita/blob/9c3e5400007f8a0282f7d883f2ad5e71716e514d/Kavita.Services/ArchiveService.cs#L278-L314","documentation":"Thrown by ArchiveService.CreateZipForDownload (line 296) when ZipFile.CreateFromDirectory(tempLocation, zipPath) throws an AggregateException — the OS-level zip creation from the staged temp folder failed. The original AggregateException is logged. Commonly this is a path/permission/disk-space problem on the destination zip or the source temp folder rather than a corrupt input.","triggerScenarios":"A download request reaches the zipping stage (copy succeeded) but System.IO.Compression cannot produce the zip: tempLocation was concurrently removed, zipPath already exists/locked, or the volume is out of space.","commonSituations":"Another thread/process deleted tempLocation between copy and zip; destination filename collision with the existing-file fast path; anti-virus scanning/locking the new zip on Windows; disk full.","solutions":["Check the logged AggregateException inner exceptions for the real IO error (UnauthorizedAccess, IOException, OutOfSpace).","Ensure the Temp directory has adequate free space and write permissions.","Guarantee the staging directory still exists at zip time (avoid concurrent ClearAndDeleteDirectory on the same key)."],"exampleFix":"// before\ncatch (AggregateException ex) {\n    logger.LogError(ex, \"There was an issue creating temp archive\");\n    throw new KavitaException(\"generic-create-temp-archive\");\n}\n\n// after — log inner exceptions for diagnosis\ncatch (AggregateException ex) {\n    foreach (var inner in ex.InnerExceptions) logger.LogError(inner, \"zip create failure\");\n    throw new KavitaException(\"generic-create-temp-archive\");\n}","handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"catch (KavitaException ex) { return BadRequest(await localizationService.TranslateAsync(UserId, ex.Message)); }\n// Log inner exceptions for diagnosis:\ncatch (AggregateException ex) { foreach (var i in ex.InnerExceptions) logger.LogError(i, \"zip inner\"); throw new KavitaException(\"generic-create-temp-archive\"); }","preventionTips":["Keep Temp volume with adequate free space and write permission.","Avoid deleting the staging tempLocation between copy and zip on the same key.","On Windows, exclude the Temp dir from AV real-time scanning if zips fail repeatedly."],"tags":["archive","download","io-error","zip","filesystem"],"backgroundTag":null,"analyzedSha":"9c3e5400007f8a0282f7d883f2ad5e71716e514d","analyzedAt":"2026-08-13T19:06:05.897Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}