четверг, 10 февраля 2022 г.

Unzip, unrar


private static final ArchiveStreamFactory FACTORY = new ArchiveStreamFactory();

private void unpackZip(Resource resource) {

    try (InputStream bufferedInputStream = new BufferedInputStream(resource.getInputStream());
ArchiveInputStream archiveInputStream = FACTORY.createArchiveInputStream(bufferedInputStream)) {
ArchiveEntry archiveEntry;

while ((archiveEntry = archiveInputStream.getNextEntry()) != null) {
String entryPath = convertToText(((ZipArchiveEntry) archiveEntry).getRawName());
log.info("archiveEntry is {}", entryPath);
if (!archiveEntry.isDirectory() && archiveInputStream.canReadEntryData(archiveEntry)) {
doSomeWork(new InputStreamResource(archiveInputStream), Paths.get(entryPath).getFileName().toString());
}
}
}
catch (IOException | ArchiveException e) {
log.error("Произошла ошибка при попытке чтения вложенного архива", e);
}

}

private String convertToText(byte[] name) {
Charset detectedCharset = IBM_866;
try {
// Apache tika used
detectedCharset =
new UniversalEncodingDetector().detect(new ByteArrayInputStream(name), new Metadata());
}
catch (IOException e) {
log.error("Error on charset detecting");
}

if (UTF_8.equals(detectedCharset)) {
return new String(name, UTF_8);
}
else {
return new String(name, IBM_866);
}
}

 private void unpackWith7Zip(Resource resource) {

    try (IInStream inStream = new ByteArrayStream(getCopyResourceStream(resource), false);
IInArchive inArchive = SevenZip.openInArchive(null, inStream)) {
for (ISimpleInArchiveItem item : inArchive.getSimpleInterface().getArchiveItems()) {
log.info("archiveEntry is {}", item.getPath());
if (!item.isFolder()) {
try (ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream()) {
ExtractOperationResult result = item.extractSlow(data -> {
try {
byteArrayOutputStream.write(data);
} catch (IOException e) {
log.error("Error write data from file {} , exception: {}", item.getPath(), e.getLocalizedMessage());
}
return data.length;
});
if (result == ExtractOperationResult.OK) {
doSomeWork(new ByteArrayResource(byteArrayOutputStream.toByteArray()), Paths.get(item.getPath()).getFileName().toString());
                    } else {
log.error("Error unpack archive {}, with type {} , status: {}", item.getPath(), inArchive.getArchiveFormat(), result);
}
}
}
}
}
catch (Exception e) {
log.error("Error extracting archive");
}

}

вторник, 1 февраля 2022 г.

OpenApi model geneartor

 Generate Java code from an OpenAPI specification located in a Maven JAR-dependency. 


    <dependencies>

        <dependency>

            <groupId>org.openapitools</groupId>

            <artifactId>jackson-databind-nullable</artifactId>

            <version>0.2.2</version>

        </dependency>

        <dependency>

            <groupId>io.swagger</groupId>

            <artifactId>swagger-annotations</artifactId>

            <version>1.6.3</version>

        </dependency>

    </dependencies>

<plugin>

    <groupId>org.openapitools</groupId>
<artifactId>openapi-generator-maven-plugin</artifactId>
<version>5.3.1</version>
<dependencies>
<dependency>
<groupId>com.api.specs</groupId>
<artifactId>api</artifactId>
<version>1.1.0</version>
</dependency>
</dependencies>
<executions>
<execution>
<goals>
<goal>generate</goal>
</goals>
<configuration>
<inputSpec>
/openapi/myservice2.yml
</inputSpec>
<configOptions>
<dateLibrary>java8</dateLibrary>
<library>spring-mvc</library>
</configOptions>
<modelPackage>ru.model</modelPackage>
<apiPackage>ru.api</apiPackage>
<generatorName>spring</generatorName>
<generateApis>false</generateApis>
<generateApiTests>false</generateApiTests>
<generateApiDocumentation>false</generateApiDocumentation>
<generateModels>true</generateModels>
<generateModelTests>false</generateModelTests>
<generateModelDocumentation>false</generateModelDocumentation>
<generateSupportingFiles>false</generateSupportingFiles>
<additionalProperties>false</additionalProperties>
<typeMappings>
<typeMapping>OffsetDateTime=LocalDateTime</typeMapping>
</typeMappings>
<importMappings>
<importMapping>java.time.OffsetDateTime=java.time.LocalDateTime</importMapping>
</importMappings>
</configuration>
</execution>
</executions>
</plugin>