好的,这是一个独特的问题。
我们从公司获取文件(每日)。这些文件从他们的服务器下载到我们的(SFTP)。我们处理的公司与第三方提供商打交道,该提供商创建文件(并缩小其大小)以加快下载速度并减少服务器上的文件大小。
我们每天从服务器下载9个文件,3组3个文件
每组文件由2个XML文件和一个“图像”文件组成
其中一个XML文件为我们提供了有关“ image ”文件的信息。
我们需要的XML文件中的信息:
<小时/> “ image ”文件本身无法使用,直到我们根据文件中每个图像的偏移量和长度将文件拆分为多个部分。图像基本上连在一起。我们需要提取这些图像才能查看它们。
偏移,长度和计数值的示例如下:
偏移:0
长度:2670
抵消:2670
长度:2670
抵消:5340
长度:2670
抵消:8010
长度:2670
计数:4
这意味着有4个(count
)个项目。第一个计数项目从offset[0]
开始,长度为length[0]
。第二项从offset[1]
开始,长度为length[1]
等等。
我需要在这些点上分割图像,这些点精确,没有错误的余地。第三方提供商不会向我们提供代码,我们将自己解决这个问题。如果没有拆分文件,图像文件是不可读的,在此之前基本没用。
<小时/> 我的问题:有没有人有办法在特定字节分割文件?
P.S。我还没有任何代码。我甚至不知道从哪里开始。我不是新编码,但我从来没有按字节进行文件分割。
我不关心它使用哪种语言。我只需要让它工作。
<小时/> 的修改
答案 0 :(得分:1)
使用的一些课程:
an article我发现这个例子非常有用。
/**
* Method that splits the data provided in fileToSplit into outputDirectory based on the
* collection of offsets and lengths provided in offsetAndLength.
*
* Example of input offsetAndLength:
* Long[][] data = new Long[][]{
* {0, 2670},
* {2670, 2670},
* {5340, 2670},
* {8010, 2670}
* };
*
* Output files will be placed in outputDirectory and named img0, img1... imgN
*
* @param fileToSplit
* @param outputDirectory
* @param offsetAndLength
* @throws IOException
*/
public static void split( Path fileToSplit, Path outputDirectory, Long[][] offsetAndLength ) throws IOException{
try (SeekableByteChannel sbc = Files.newByteChannel(fileToSplit, StandardOpenOption.READ )){
for(int x = 0; x < offsetAndLength.length; x++){
ByteBuffer buffer = ByteBuffer.allocate(offsetAndLength[x][4].intValue());
sbc.position(offsetAndLength[x][0]);
sbc.read(buffer);
buffer.flip();
File img = new File(outputDirectory.toFile(), "img"+x);
img.createNewFile();
try(FileChannel output = FileChannel.open(img.toPath(), StandardOpenOption.WRITE)){
output.write(buffer);
}
buffer.clear();
}
}
}
我将解析XML文件给你。